2010-05-20 58 views
0

我是新来的流利NHibernate,我遇到了问题。流利的NHibernate/SQL Server 2008插入查询问题

我已经映射定义如下:

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()(是,可怕的名字)已线路2 == 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 

回答

3

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