流畅的NHibernate/SQL Server 2008插入查询问题

发布于 2024-09-02 10:09:46 字数 4102 浏览 2 评论 0原文

我是 Fluent NHibernate 的新手,遇到了问题。

我有一个映射定义如下:

public PersonMapping()
    {
        Id(p => p.Id).GeneratedBy.HiLo("1000");
        Map(p => p.FirstName).Not.Nullable().Length(50);

        Map(p => p.MiddleInitial).Nullable().Length(1);

        Map(p => p.LastName).Not.Nullable().Length(50);
        Map(p => p.Suffix).Nullable().Length(3);
        Map(p => p.SSN).Nullable().Length(11);
        Map(p => p.BirthDate).Nullable();
        Map(p => p.CellPhone).Nullable().Length(12);
        Map(p => p.HomePhone).Nullable().Length(12);
        Map(p => p.WorkPhone).Nullable().Length(12);
        Map(p => p.OtherPhone).Nullable().Length(12);
        Map(p => p.EmailAddress).Nullable().Length(50);
        Map(p => p.DriversLicenseNumber).Nullable().Length(50);

        Component<Address>(p => p.CurrentAddress, m =>
        {
            m.Map(p => p.Line1, "Line1").Length(50);
            m.Map(p => p.Line2, "Line2").Length(50);
            m.Map(p => p.City, "City").Length(50);
            m.Map(p => p.State, "State").Length(50);
            m.Map(p => p.Zip, "Zip").Length(2);
        });

        Map(p => p.EyeColor).Nullable().Length(3);
        Map(p => p.HairColor).Nullable().Length(3);
        Map(p => p.Gender).Nullable().Length(1);
        Map(p => p.Height).Nullable();
        Map(p => p.Weight).Nullable();
        Map(p => p.Race).Nullable().Length(1);
        Map(p => p.SkinTone).Nullable().Length(3);
        HasMany(p => p.PriorAddresses).Cascade.All();
    }


    public PreviousAddressMapping()
    {
        Table("PriorAddress");

        Id(p => p.Id).GeneratedBy.HiLo("1000");
        Map(p => p.EndEffectiveDate).Not.Nullable();
        Component<Address>(p => p.Address, m =>
        {
            m.Map(p => p.Line1, "Line1").Length(50);
            m.Map(p => p.Line2, "Line2").Length(50);
            m.Map(p => p.City, "City").Length(50);
            m.Map(p => p.State, "State").Length(50);
            m.Map(p => p.Zip, "Zip").Length(2);
        });

    }

我的测试是

    [Test]
    public void can_correctly_map_Person_with_Addresses()
    {
        var myPerson = new Person("Jane", "", "Doe");
        var priorAddresses = new[]
        {   
            new PreviousAddress(ObjectMother.GetAddress1(), DateTime.Parse("05/13/2010")),
            new PreviousAddress(ObjectMother.GetAddress2(), DateTime.Parse("05/20/2010"))
        };

        new PersistenceSpecification<Person>(Session)
            .CheckProperty(c => c.FirstName, myPerson.FirstName)
            .CheckProperty(c => c.LastName, myPerson.LastName)
            .CheckProperty(c => c.MiddleInitial, myPerson.MiddleInitial)
            .CheckList(c => c.PriorAddresses, priorAddresses)

            .VerifyTheMappings();
    }

GetAddress1() (是的,可怕的名字)有 Line2 == null

这些表似乎在 sql server 2008 中正确创建,但测试失败并出现 SQLException“字符串或二进制数据将被截断。”当我在 SQL Profiler 中获取 sql 语句时,我注意到

exec sp_executesql N'INSERT INTO PriorAddress (Line1, Line2, City, State, Zip, 
EndEffectiveDate, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)',N'@p0 
nvarchar(18),@p1 nvarchar(4000),@p2 nvarchar(10),@p3 nvarchar(2),@p4 nvarchar(5),@p5 
datetime,@p6 int',@p0=N'6789 Somewhere Rd.',@p1=NULL,@p2=N'Hot 
Coffee',@p3=N'MS',@p4=N'09876',@p5='2010-05-13 00:00:00',@p6=1001

@p1 参数被设置为 nvarchar(4000) 并传递了 NULL 值。

为什么将参数设置为 nvarchar(4000)?我该如何修复它?

谢谢!

我认为它与 Line2 参数有关的第一个理论是错误的。我向 Line2 添加了一个值,重新运行测试,但仍然遇到相同的错误。

exec sp_executesql N'INSERT INTO PriorAddress 
       (Line1, Line2, City, State, Zip, EndEffectiveDate, Id) 
VALUES (@p0,   @p1,   @p2,   @p3,  @p4, @p5,              @p6)',
N'@p0 nvarchar(18),@p1 nvarchar(6),@p2 nvarchar(10),@p3 nvarchar(2),@p4 
nvarchar(5),@p5 datetime,@p6 int',
@p0=N'6789 Somewhere Rd.',
@p1=N'A test',
@p2=N'Hot Coffee',
@p3=N'MS',
@p4=N'09876',
@p5='2010-05-13 00:00:00',
@p6=1001

I'm new to Fluent NHibernate and I'm running into a problem.

I have a mapping defined as follows:

public PersonMapping()
    {
        Id(p => p.Id).GeneratedBy.HiLo("1000");
        Map(p => p.FirstName).Not.Nullable().Length(50);

        Map(p => p.MiddleInitial).Nullable().Length(1);

        Map(p => p.LastName).Not.Nullable().Length(50);
        Map(p => p.Suffix).Nullable().Length(3);
        Map(p => p.SSN).Nullable().Length(11);
        Map(p => p.BirthDate).Nullable();
        Map(p => p.CellPhone).Nullable().Length(12);
        Map(p => p.HomePhone).Nullable().Length(12);
        Map(p => p.WorkPhone).Nullable().Length(12);
        Map(p => p.OtherPhone).Nullable().Length(12);
        Map(p => p.EmailAddress).Nullable().Length(50);
        Map(p => p.DriversLicenseNumber).Nullable().Length(50);

        Component<Address>(p => p.CurrentAddress, m =>
        {
            m.Map(p => p.Line1, "Line1").Length(50);
            m.Map(p => p.Line2, "Line2").Length(50);
            m.Map(p => p.City, "City").Length(50);
            m.Map(p => p.State, "State").Length(50);
            m.Map(p => p.Zip, "Zip").Length(2);
        });

        Map(p => p.EyeColor).Nullable().Length(3);
        Map(p => p.HairColor).Nullable().Length(3);
        Map(p => p.Gender).Nullable().Length(1);
        Map(p => p.Height).Nullable();
        Map(p => p.Weight).Nullable();
        Map(p => p.Race).Nullable().Length(1);
        Map(p => p.SkinTone).Nullable().Length(3);
        HasMany(p => p.PriorAddresses).Cascade.All();
    }


    public PreviousAddressMapping()
    {
        Table("PriorAddress");

        Id(p => p.Id).GeneratedBy.HiLo("1000");
        Map(p => p.EndEffectiveDate).Not.Nullable();
        Component<Address>(p => p.Address, m =>
        {
            m.Map(p => p.Line1, "Line1").Length(50);
            m.Map(p => p.Line2, "Line2").Length(50);
            m.Map(p => p.City, "City").Length(50);
            m.Map(p => p.State, "State").Length(50);
            m.Map(p => p.Zip, "Zip").Length(2);
        });

    }

My test is

    [Test]
    public void can_correctly_map_Person_with_Addresses()
    {
        var myPerson = new Person("Jane", "", "Doe");
        var priorAddresses = new[]
        {   
            new PreviousAddress(ObjectMother.GetAddress1(), DateTime.Parse("05/13/2010")),
            new PreviousAddress(ObjectMother.GetAddress2(), DateTime.Parse("05/20/2010"))
        };

        new PersistenceSpecification<Person>(Session)
            .CheckProperty(c => c.FirstName, myPerson.FirstName)
            .CheckProperty(c => c.LastName, myPerson.LastName)
            .CheckProperty(c => c.MiddleInitial, myPerson.MiddleInitial)
            .CheckList(c => c.PriorAddresses, priorAddresses)

            .VerifyTheMappings();
    }

GetAddress1() (yeah, horrible name) has Line2 == null

The tables seem to be created correctly in sql server 2008, but the test fails with a SQLException "String or binary data would be truncated." When I grab the sql statement in SQL Profiler, I get

exec sp_executesql N'INSERT INTO PriorAddress (Line1, Line2, City, State, Zip, 
EndEffectiveDate, Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)',N'@p0 
nvarchar(18),@p1 nvarchar(4000),@p2 nvarchar(10),@p3 nvarchar(2),@p4 nvarchar(5),@p5 
datetime,@p6 int',@p0=N'6789 Somewhere Rd.',@p1=NULL,@p2=N'Hot 
Coffee',@p3=N'MS',@p4=N'09876',@p5='2010-05-13 00:00:00',@p6=1001

Notice the @p1 parameter is being set to nvarchar(4000) and being passed a NULL value.

Why is it setting the parameter to nvarchar(4000)? How can I fix it?

Thanks!

My first theory that it was related to the Line2 parameter was wrong. I added a value to Line2, re-ran the test, and I'm still getting the same error.

exec sp_executesql N'INSERT INTO PriorAddress 
       (Line1, Line2, City, State, Zip, EndEffectiveDate, Id) 
VALUES (@p0,   @p1,   @p2,   @p3,  @p4, @p5,              @p6)',
N'@p0 nvarchar(18),@p1 nvarchar(6),@p2 nvarchar(10),@p3 nvarchar(2),@p4 
nvarchar(5),@p5 datetime,@p6 int',
@p0=N'6789 Somewhere Rd.',
@p1=N'A test',
@p2=N'Hot Coffee',
@p3=N'MS',
@p4=N'09876',
@p5='2010-05-13 00:00:00',
@p6=1001

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-09-09 10:09:46

对不起,我是个白痴。我想通了。 Zip 的长度应该大于 2。

Sorry, I'm an idiot. I figured it out. Zip should probably have a larger length than 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文