nhibernate 动态插入因某些空组件属性而失败
我有一个不接受空值并且每个字段都有默认值的数据库。使用流畅的 nHibernate,如果我有一个组件填充了一些但未填充所有属性,我会在插入时收到错误。我只是想知道如何将 DynamicInsert 标志降低到组件级别。也许已经晚了,但我会把它扔在这里,看看它去哪儿了。
映射:
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(""); });
}
测试:
Client nw = new Client{ Address = new Address{Streetaddress = "my address"},Accountnumber = "6543219",Transferaccount = "1"};
IRepository repo2 = new Repository(session);
repo2.Save(nw);
错误:
无法插入:[BusinessObjects.Client#BusinessObjects.Client][SQL:插入客户端(coaddress、cocity、cozip、costate、acct_no、tr_acct_no)值(?、?、?、?、?、 ?)]
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 (?, ?, ?, ?, ?, ?)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定,但我认为您指定的“NotNullable”和“Default”映射属性只有影响/仅在您使用 NHibernate 映射创建数据库模式时使用,并且它们没有实际效果NHibernate 将向您的数据库插入什么内容。
如果您想要某些属性的默认值,我认为您应该在具有该属性的类的构造函数中自己为该属性指定默认值。
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:
Object Property:
In the mapping I was doing:
But I changed it to:
Which doesn't insert the property if it is null
NHibernate 本身不支持组件映射上的动态插入——这在 Fluent 中并不缺少。
建议您在 Address 上放置一个默认构造函数,将这些默认属性值设置为空字符串而不是 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 将组件视为单个组件...组件:-)
这意味着它不会对组件进行动态插入。
NHibernate considers a component to be a single well... component :-)
That means that it will not do a dynamic insert for components.