2010-05-20 82 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.