c# - Fluent NHibernate 继承映射类型

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

我是 Fluent NHibernate 的新手,到目前为止,除了继承部分之外,我设法让我的映射正常工作。有没有人可以帮我完成映射?我已经尽可能地简化了代码。

谢谢!

我的数据库:

CREATE TABLE [User] (
UserID                  INT             NOT NULL IDENTITY(1,1),
Type                    CHAR(1)         NOT NULL,
Email                   VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID)
);

CREATE TABLE [Student] (
UserID                  INT             NOT NULL,
Firstname               VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID)               
);

CREATE TABLE [Company] (
UserID                  INT             NOT NULL,
Name                    VARCHAR(255)    NOT NULL,
PRIMARY KEY(UserID),
FOREIGN KEY(UserID) REFERENCES [User](UserID),
);

我的类(class):

public class User
{
    public virtual int UserID { get; set; }
    public virtual UserType Type { get; set; }
    public virtual string Email { get; set; }
    public User()
    {
    }
}

public class Student : User
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public Student() 
        : base()
    {

    }
}

public class Company : User
{
    public virtual string Name { get; set; }
    public Company() 
        : base()
    {
    }
}

public enum UserType
{
    STUDENT = 0,
    COMPANY = 1
}

映射:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("[User]");
        Id(x => x.UserID);
        Map(x => x.Type).CustomType<int>();
        Map(x => x.Email);
    }
}

public class CompanyMap : ClassMap<Company>
{
    public CompanyMap()
    {
        Table("Company");
        Id(x => x.UserID);
        Map(x => x.Name);

    }
}

public class StudentMap: ClassMap<Student>
{
    public StudentMap()
    {
        Table("Student");
        Id(x => x.UserID);
        Map(x => x.Firstname);
        Map(x => x.Lastname);    
    }
}

最佳答案

Internet 上很少有关于Fluent mapping 和 NHibernate 继承的好文章。其中之一是关于mapping-by-code,但它也提供了关于Fluent mapping的详细解释(只需向下滚动)

Mapping-by-Code - inheritance 作者:亚当·巴尔

与您的场景相关的小摘录

... Table per class

The second strategy for mapping inheritance is table per class with joined subclasses. In this option subclasses are stored in separate tables that have foreign key to base class table and are joined with the table for base class, if needed. In this case, in mapping-by-code, we have to map subclasses by inheriting from JoinedSubclassMapping. Here is the example of joined subclass mapping with all available options:

public class CompanyMap : JoinedSubclassMapping<Company>
{
    public CompanyMap()
    {
        Key(k =>
        {
            k.Column("PartyId");
            // or...
            k.Column(c =>
            {
                c.Name("PartyId");
                // etc.
            });

            k.ForeignKey("party_fk");
            k.NotNullable(true);
            k.OnDelete(OnDeleteAction.Cascade); // or OnDeleteAction.NoAction
            k.PropertyRef(x => x.CompanyName);
            k.Unique(true);
            k.Update(true);
        });

        Property(x => x.CompanyName);
    }
}

另一篇非常好的综合文章:

Inheritance mapping strategies in Fluent Nhibernate 作者:伊戈尔·伊格纳托夫


但是,我建议:

Do not go this way. Do NOT use inheritance if possible. If you have to - do not use so deep inheritance.

请务必阅读以下内容:

Composition over inheritance

小引用:

Benefits

To favor composition over inheritance is a design principle that gives the design higher flexibility, giving business-domain classes and more stable business domain in the long term. In other words, HAS-A can be better than an IS-A relationship.

Initial design is simplified by identifying system object behaviors in separate interfaces instead of creating a hierarchical relationship to distribute behaviors among business-domain classes via inheritance. This approach more easily accommodates future requirements changes that would otherwise require a complete restructuring of business-domain classes in the inheritance model. Additionally, it avoids problems often associated with relatively minor changes to an inheritance-based model that includes several generations of classes.

NHibernate 确实是一个非常棒的工具,几乎支持我们的任何一种愿望......但它仍然不意味着我们应该使用它。

关于c# - Fluent NHibernate 继承映射类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156268/

相关文章:

.net - 设置 NLS_LANG 以进行 Oracle 管理的数据访问

nhibernate - 从 nhibernate 中的 INSERT 命令中排除属性

c# - NHibernate Criteria QueryByExample 卡在中间的 SQL

nhibernate - 流畅的 NHibernate 缓存与自动映射

c# - 尝试删除多对多关系中两个对象之间的连接时出现的问题

c# - 使用 JSON.NET 解析嵌套的 JSON 对象

c# - 从 std::fstream 读取 float 返回负零

c# - 如何在 vb.net 中使用 linq 对另一个列表中的列表进行排序

c# - 为什么在这种情况下为 for 循环使用指针性能更高?

c# - 删除和添加相同数据库后未创建 Fluent Nhibernate 映射