gpt4 book ai didi

c# - Fluent NHibernate 继承映射类型

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:14 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com