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

时间:2010-05-20 14:58:26

标签: nhibernate fluent-nhibernate

我是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事件探查器中获取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

1 个答案:

答案 0 :(得分:3)

抱歉,我是个白痴。我想到了。 Zip的长度应该大于2。