Fluent nHibernate 映射问题

发布于 2024-11-14 13:02:47 字数 1479 浏览 1 评论 0原文

我有这些表/类(示例):

table FirstTable (
    Id INTEGER NOT NULL DEFAULT AUTOINCREMENT, 
    Name VARCHAR(100) NOT NULL,
    Document VARCHAR(20) NOT NULL
)

table SecondTable (
    Id INTEGER NOT NULL,
    Something VARCHAR(100) NULL,
    FOREIGN KEY (Id) REFERENCES FirstTable (Id)
)

public class FirstClass {
    public string Name { get; set; }
    public string Document { get; set; }
    public SecondClass SecondClass { get; set; }
}

public class SecondClass {
    public string Something { get; set; }
    public FirstClass FirstClass { get; set; }
}

映射是:

public class FirstClassMap : ClassMap<FirtsClass> {
    Table("FirstTable");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Name);
    Map(x => x.Document);
    References(x => x.SecondClass, "Id").ForeignKey();
}

public class SecondClassMap : ClassMap<SecondClass> {
    Table("SecondTable");
    Id(x => x.Id).GeneratedBy.Foreign("FirstClass");
    Max(x => x.Something);
    HasOne(x => x.FirstClass).PropertyRef(x => x.SecondClass).Cascade.SaveUpdate();
}

FirstClass 可以有 (0,1) SecondClass,SecondClass 可以有 (1,1) FirstClass。

以下代码返回错误“尝试从 null 一对一属性分配 id:SecondClass”

var test = new SecondClass();
test.FirstClass = new FirstClass();

test.Something = "New test";
test.FirstClass.Name = "My name";
test.FirstClass.Document = "My document";
// ... commands to save.

I have these tables/classes (example):

table FirstTable (
    Id INTEGER NOT NULL DEFAULT AUTOINCREMENT, 
    Name VARCHAR(100) NOT NULL,
    Document VARCHAR(20) NOT NULL
)

table SecondTable (
    Id INTEGER NOT NULL,
    Something VARCHAR(100) NULL,
    FOREIGN KEY (Id) REFERENCES FirstTable (Id)
)

public class FirstClass {
    public string Name { get; set; }
    public string Document { get; set; }
    public SecondClass SecondClass { get; set; }
}

public class SecondClass {
    public string Something { get; set; }
    public FirstClass FirstClass { get; set; }
}

The mapping is:

public class FirstClassMap : ClassMap<FirtsClass> {
    Table("FirstTable");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Name);
    Map(x => x.Document);
    References(x => x.SecondClass, "Id").ForeignKey();
}

public class SecondClassMap : ClassMap<SecondClass> {
    Table("SecondTable");
    Id(x => x.Id).GeneratedBy.Foreign("FirstClass");
    Max(x => x.Something);
    HasOne(x => x.FirstClass).PropertyRef(x => x.SecondClass).Cascade.SaveUpdate();
}

FirstClass can have (0,1) SecondClass, and SecondClass can have (1,1) FirstClass.

The bellow code return the error "attempted to assign id from null one-to-one property: SecondClass"

var test = new SecondClass();
test.FirstClass = new FirstClass();

test.Something = "New test";
test.FirstClass.Name = "My name";
test.FirstClass.Document = "My document";
// ... commands to save.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

掌心的温暖 2024-11-21 13:02:47

NH 似乎试图先保存 SecondClass,但无法从尚未保存的 FirstClass 中获取生成的 ID。

尝试将 .Cascade.SaveUpdate() 移至 FirstClassMap 中的 References 声明,并在 FirstClass 上调用保存命令。

It seems like NH is trying to save SecondClass first and fails to grab a generated ID from the not yet saved FirstClass.

Try to move the .Cascade.SaveUpdate() to the References declaration in FirstClassMap and call the save command on FirstClass.

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