Fluent NHibernate关系映射和保存异常

发布于 2024-10-21 04:24:30 字数 2012 浏览 0 评论 0原文

我是 NHibernate 的新手,正在尝试使用 Fluent 的 AutoMapping 功能,这样我就不需要手动维护单独的 XML 文件。不幸的是,我遇到了引用实体的问题,特别是“Fluent_NHibernate_Demo.Domain.Name.Id的getter发生异常” - System.Reflection.TargetException:对象与目标类型不匹配< /强>。

我似乎至少在我的一个映射类中存在错误,尽管它们确实生成了正确的 SQL(即创建的表具有正确的索引)。

我的域模型和映射的实现是:


Name.cs

public class Name
{
    public virtual int Id { get; protected set; }
    public virtual string First { get; set; }
    public virtual string Middle { get; set; }
    public virtual string Last { get; set; }
}

Person.cs

public class Person
{
    public virtual int Id { get; protected set; }
    public virtual Name Name { get; set; }
    public virtual short Age { get; set; }
}

NameMap.cs

public NameMap()
{
    Table("`Name`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    Map(x => x.First).Column("`First`").Not.Nullable().Length(20);
    Map(x => x.Middle).Column("`Middle`").Nullable().Length(20);
    Map(x => x.Last).Column("`Last`").Not.Nullable().Length(20);
}

PersonMap.cs

public PersonMap()
{
    Table("`Person`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    References<Name>(x => x.Name.Id, "`NameId`").Not.Nullable();
    // There's no exception if the following line is used instead of References
    // although no constraint is created
    // Map(x => x.Name.Id).Column("`NameId`").Not.Nullable();
    Map(x => x.Age).Column("`Age`").Nullable();
}

最后,以下代码将产生异常:

Name name = new Name { First = "John", Last = "Doe" };
session.Save(name);
Person person = new Person { Name = name, Age = 22 };
session.Save(person); // this line throws the exception

如上所述,创建的模式是正确的,但我无法使用上面的代码进行保存。使用 Fluent NHibernate 创建外键约束的正确方法是什么?

I'm new to NHibernate and am attempting to use Fluent's AutoMapping capability so that I do not need to maintain separate XML files by hand. Unfortunately I'm running into a problem with referenced entities, specifically 'Exception occurred getter of Fluent_NHibernate_Demo.Domain.Name.Id' - System.Reflection.TargetException: Object does not match target type.

I appear to have an error in at least one of my mapping classes although they do generate the correct SQL (i.e. the created tables have the correct indexes).

The implementations for my domain models and mappings are:


Name.cs

public class Name
{
    public virtual int Id { get; protected set; }
    public virtual string First { get; set; }
    public virtual string Middle { get; set; }
    public virtual string Last { get; set; }
}

Person.cs

public class Person
{
    public virtual int Id { get; protected set; }
    public virtual Name Name { get; set; }
    public virtual short Age { get; set; }
}

NameMap.cs

public NameMap()
{
    Table("`Name`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    Map(x => x.First).Column("`First`").Not.Nullable().Length(20);
    Map(x => x.Middle).Column("`Middle`").Nullable().Length(20);
    Map(x => x.Last).Column("`Last`").Not.Nullable().Length(20);
}

PersonMap.cs

public PersonMap()
{
    Table("`Person`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    References<Name>(x => x.Name.Id, "`NameId`").Not.Nullable();
    // There's no exception if the following line is used instead of References
    // although no constraint is created
    // Map(x => x.Name.Id).Column("`NameId`").Not.Nullable();
    Map(x => x.Age).Column("`Age`").Nullable();
}

Finally, the following code will produce the exception:

Name name = new Name { First = "John", Last = "Doe" };
session.Save(name);
Person person = new Person { Name = name, Age = 22 };
session.Save(person); // this line throws the exception

As mentioned, the created schema is correct but I'm unable to save using the above code. What is the correct way to create a foreign key constraint using Fluent NHibernate?

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

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

发布评论

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

评论(1

蓝咒 2024-10-28 04:24:30

如果您想通过 ID 引用名称,那么您应该这样做。 NHibernate 足够聪明,可以弄清楚 Person 上的实际 FK 字段应该是什么以及它应该指向哪里;毕竟,这就是 ORM 旨在执行的工作。

试试这个映射:

public PersonMap()
{
    Table("`Person`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    References(x => x.Name, "`NameId`").Not.Nullable();        
    Map(x => x.Age).Column("`Age`").Nullable();
}

你已经映射了人和名字;这样,NHibernate 就知道哪个属性是 Name 的 ID 属性,并且可以创建并遍历 Person 上的外键。

If you want to reference the name, by ID, then that's what you should do. NHibernate is smart enough to figure out what the actual FK field on Person should be and where it should point; that is, after all, the job an ORM is designed to perform.

Try this mapping:

public PersonMap()
{
    Table("`Person`");
    Id(x => x.Id).Column("`Id`").GeneratedBy.Identity();
    References(x => x.Name, "`NameId`").Not.Nullable();        
    Map(x => x.Age).Column("`Age`").Nullable();
}

You've mapped the person and the name; as a result, NHibernate knows which property is the ID property of Name, and can create and traverse the foreign key on Person.

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