【问题标题】:NHibernate unidirectional one-to-many relationship not saving foreign keyNHibernate 单向一对多关系不保存外键
【发布时间】:2015-07-24 08:47:51
【问题描述】:

我是 NHibernate 的新手,我正在创建一个简单的场景来测试框架功能。

我必须要基本实体:

public class School : BaseEntity
{
    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
}

public class Student : BaseEntity
{
    public virtual string Name { get; set; }
    public virtual string Surname { get; set; }
    public virtual string Email { get; set; }
    public virtual School School { get; set; }
}

从一个简单的基类继承:

public abstract class BaseEntity
{
    public virtual int Id { get; protected set; }
}

比我这样使用 FluentNhibernate 映射实体:

return Fluently.Configure()
   .Database(MsSqlConfiguration.MsSql2012.ConnectionString(
        c => c.FromConnectionStringWithKey("DataModel")))
   .Mappings(m => m.AutoMappings
       .Add(AutoMap.AssemblyOf<BaseEntity>()
       .Where(t => t.Namespace == "MyApp.Models"))
       .IgnoreBase<BaseEntity>()
       .Override<User>(map =>
       {
           map.Table("Users");
           map.HasOne<School>(u => u.School).ForeignKey("SchoolId");
       })
       .Override<School>(map =>
       {
           map.Table("Schools");
       })
   ))
   .BuildSessionFactory();

我的测试代码很简单:

using (var transaction = DbSession.BeginTransaction())
{
    Student u1 = DbSession.Get<Student>("user-id");
    School s1 = DbSession.Get<School>("school-id");

    u1.School = s1; // updating the associated school

    DbSession.SaveOrUpdate(u1);

    transaction.Commit(); // !!! the foreign key is not updated
}

检查学生表,该行未使用新的学校 ID 更新。

那么,我的代码有什么问题?我的映射中是否存在不正确(或缺失)的内容?

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping


    【解决方案1】:

    属于SchoolStudentmany-to-one 关系。

    5.1.11. many-to-one

    使用多对一元素声明与另一个持久类的普通关联。关系模型是多对一的关联。 (实际上只是一个对象引用。)

    它的流利版本是.References()

    References / many-to-one

    引用用于在两个实体之间创建多对一关系,并应用于“多方”。您正在引用单个其他实体,因此您使用 References 方法。 #HasMany / 一对多是引用关系的“另一边”,并被应用在“一边”。

    让我们绘制一本书与其作者之间的关系。

    public class Book
    {
      public Author Author { get; set; }
    }
    
    public class Author
    {
      public IList<Book> Books { get; set; }
    }
    

    在领域术语中,我们有一个作者,它可以与任意数量的书籍相关联,而每本书都可以与一个作者相关联。

    在数据库术语中,我们有一个 book 表,其中的外键列引用了 author 表的主键。

    要在您的 Book #ClassMap 中创建引用关系,请在 BookMap 构造函数中添加以下调用:

    References(x => x.Author);
    

    换句话说,如果我们需要many-to-one关系映射到fluent,我们不能使用.HasOne(),但是.References()

    //map.HasOne<School>(u => u.School).ForeignKey("SchoolId");
    map.References(u => u.School, "SchoolId");
    

    要全面了解 .References() API,请阅读本文的后半部分(前半部分是代码映射,后半部分是与 fluent 的比较)

    mapping by code - Many-to-One 亚当·巴

    注意 - 什么是.HasOne()one-to-one)场景问题可以在here

    找到

    【讨论】:

    • 你完全正确!我从错误的角度看问题!谢谢。
    • 这有帮助吗?太棒了 ;) 享受强大的 NHibernate,先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多