简体   繁体   English

NHibernate单向一对多关系不保存外键

[英]NHibernate unidirectional one-to-many relationship not saving foreign key

I'm new to NHibernate and I'm creating a simple scenario to test the framework functionalities. 我是NHibernate的新手,我正在创建一个简单的场景来测试框架功能。

I have to basic entities: 我要基本实体:

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; }
}

inheriting from a simple base class: 从简单的基类继承:

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

Than I map entities using FluentNhibernate this way: 比我使用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();

My test code is very simple: 我的测试代码非常简单:

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
}

Checkign the Students table, the row is not updated with the new school id. 检查“学生”表,该行未使用新的学校ID更新。

So, what's wrong in my code? 那么,我的代码有什么问题呢? Is there something incorrect (or missing) in my mappings? 我的映射中是否有不正确(或丢失)的东西?

A Student belonging to the School is a many-to-one relationship. 属于School Studentmany-to-one关系。

5.1.11. 5.1.11。 many-to-one 许多到一

An ordinary association to another persistent class is declared using a many-to-one element. 使用多对一元素声明与另一个持久类的普通关联。 The relational model is a many-to-one association. 关系模型是多对一关联。 (It's really just an object reference.) (它实际上只是一个对象引用。)

Its fluent version is .References() 它的流利版本是.References()

References / many-to-one 参考/多对一

References is for creating many-to-one relationships between two entities, and is applied on the "many side." 引用用于在两个实体之间创建多对一关系,并应用于“许多方面”。 You're referencing a single other entity, so you use the References method. 您引用的是其他单个实体,因此您使用了References方法。 #HasMany / one-to-many is the "other side" of the References relationship, and gets applied on the "one side." #HasMany /一对多是“引用”关系的“另一侧”,并应用于“一侧”。

Let's map a relationship between a book and its author. 让我们来映射一本书与其作者之间的关系。

public class Book
{
  public Author Author { get; set; }
}

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

In domain terms, we have an Author which may be associated with any number of Books, and Books, each of which can be associated with a single Author. 在域方面,我们有一个Author(作者)可以与任意数量的Book相关联,并且Books(每本可以与一个Author相关联)。

In database terms, we'd have a book table with a foreign key column referencing the primary key of an author table. 用数据库术语来说,我们将有一个带有外键列的书表,该外键列引用了作者表的主键。

To create the references relationship in your Book #ClassMap, add the following call in the BookMap constructor: 要在您的Book #ClassMap中创建引用关系,请在BookMap构造函数中添加以下调用:

References(x => x.Author);

Other words, if we need the many-to-one relationship to be mapped with fluent, we cannot use .HasOne() but .References() 换句话说,如果我们需要使用流畅many-to-one关系进行映射,则不能使用.HasOne()而可以使用.References()

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

To get full overview of the .References() API, read the second half of this article (the first half is bout mapping by code, the second is comparison with fluent) : 要全面了解.References()API,请阅读本文的后半部分(上半部分是通过代码进行bout映射,第二部分是与fluent的比较)

mapping by code - Many-to-One by Adam Bar 通过代码映射 -Adam Bar 多对一

Note - what is the .HasOne() (the one-to-one ) scenario issues could be found here 注意-什么是.HasOne()one-to-one )方案问题,可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM