views:

42

answers:

1

Hi all,

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
+2  A: 

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

Mark