开发者

nhibernate Dynamic Insert fails with some null Component properties

开发者 https://www.devze.com 2022-12-14 13:41 出处:网络
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

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 ho开发者_如何学编程w 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 (?, ?, ?, ?, ?, ?)]


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.


Just ran into a similar problem. The Database has a default constraint:

CREATE TABLE MyTable
(
...
    InsertDate datetime not null default getdate()
...
)

Object Property:

public DateTime? InsertDate{get;set;}

In the mapping I was doing:

Map(x => x.InsertDate).Column("InsertDate").Not.Nullable().Default("getdate()");

But I changed it to:

Map(x => x.InsertDate).Column("InsertDate").Generated.Insert();

Which doesn't insert the property if it is null


NHibernate itself doesn't support dynamic insert on a component mapping - it's not something missing in Fluent.

Suggest you put a default constructor on Address that sets these default property values to empty strings rather than nulls?


NHibernate considers a component to be a single well... component :-) That means that it will not do a dynamic insert for components.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号