nhibernate 动态插入因某些空组件属性而失败

发布于 2024-08-15 08:07:12 字数 1206 浏览 2 评论 0原文

我有一个不接受空值并且每个字段都有默认值的数据库。使用流畅的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

止于盛夏 2024-08-22 08:07:12

我不确定,但我认为您指定的“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.

稀香 2024-08-22 08:07:12

刚刚遇到类似的问题。
数据库有一个默认约束:

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

对象属性:

public DateTime? InsertDate{get;set;}

在我正在做的映射中:

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

但我将其更改为:

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

如果属性为空,则不会插入属性

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

冬天的雪花 2024-08-22 08:07:12

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?

丿*梦醉红颜 2024-08-22 08:07:12

NHibernate 将组件视为单个组件...组件:-)
这意味着它不会对组件进行动态插入。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文