R – nhibernate Dynamic Insert fails with some null Component properties

fluent-nhibernatenhibernatenhibernate-mapping

I have a database that accepts no null values and has a default for every field. Using fluent nHibernate, I am getting an error on an Insert if I have a component that has some, but not all properties filled out. I am just wondering how to get the DynamicInsert flag down to the component level. Perhaps it is late, but I'll just drop this off here and see where it goes.

mapping:

 public ClientMap()
    {
        Table("Clients");
        DynamicInsert();
        DynamicUpdate();
        CompositeId()
            .KeyProperty(x => x.Accountnumber, "acct_no")
            .KeyProperty(x => x.Transferaccount, "tr_acct_no");

        Component(c => c.Address,
            m =>{
                m.Map(x => x.Streetaddress, "coaddress").Not.Nullable().Default("");
                m.Map(x => x.City, "cocity").Not.Nullable().Default("");
                m.Map(x => x.Postalcode, "costate").Not.Nullable().Default("");
                m.Map(x => x.State, "cozip").Not.Nullable().Default(""); });
    }

test:

Client nw = new Client{ Address = new Address{Streetaddress = "my address"},Accountnumber = "6543219",Transferaccount = "1"};
    IRepository repo2 = new Repository(session);
    repo2.Save(nw);

error:

could not insert: [BusinessObjects.Client#BusinessObjects.Client][SQL: INSERT INTO Clients (coaddress, cocity, cozip, costate, acct_no, tr_acct_no) VALUES (?, ?, ?, ?, ?, ?)]

Best Answer

I'm not sure, but I think that the 'NotNullable' and 'Default' mapping properties that you have specified, only have influence / are only used when you create a DB schema using your NHibernate mapping, and that they have no real effect on what NHibernate will insert into your DB.

If you want a default value for some property, I think that you should give that property the default value yourself in the constructor of the class that has this property.

Related Topic