如何使用 Fluent NHibernate 将一对一的父子表折叠到类中?
如何将这些现有表映射到下面的类?
我有以下表格:
CREATE TABLE dbo.UserContact (
UserContactId int NOT NULL IDENTITY (1, 1),
UserId int NOT NULL,
ContactId int NOT NULL,
UserContactTypeId int NOT NULL,
FromDt datetime NULL,
ThruDt datetime NULL,
CreateDt datetime NOT NULL,
UpdateDt datetime NULL,
IsDeleted bit NULL,
CanSolicit bit NOT NULL
)
CREATE TABLE dbo.Contact (
ContactId int NOT NULL IDENTITY (1, 1),
CreateDt datetime NOT NULL,
UpdateDt datetime NULL
)
CREATE TABLE dbo.Electronic (
ContactId int NOT NULL,
URL nvarchar(512) NOT NULL,
ElectronicType smallint NULL
)
CREATE TABLE dbo.Phone (
ContactId int NOT NULL,
AreaCode nchar(3) NOT NULL,
PhoneNb nchar(7) NOT NULL,
Extension nchar(6) NULL,
PhoneType smallint NULL
)
CREATE TABLE dbo.Postal
(
ContactId int NOT NULL,
Street nvarchar(256) NOT NULL,
Specifier nvarchar(256) NULL,
GeocodeId int NULL
)
表“电子”、“电话”和“邮政”与“联系人”具有一对一的关系。表UserContact与Contact是多对一的; UserContact是User和Contact之间的关联表。
我还有以下课程:
public class Electronic : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Url { get; set; }
public ElectronicType Type { get; set; }
}
public class Postal : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Street { get; set; }
public string Specifier { get; set; }
public Geocode Geocode { get; set; }
}
public class Phone : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string AreaCode { get; set; }
public string PhoneNb { get; set; }
public string Extension { get; set; }
public PhoneType Type { get; set; }
}
public class UserContact : IntegerKeyEntity
{
private ICollection<Electronic> electronics = new HashSet<Electronic>();
private ICollection<Phone> phones = new HashSet<Phone>();
private ICollection<Postal> postals = new HashSet<Postal>();
// props
public virtual IEnumerable<Electronic> Electronics { get { return electronics; } }
public virtual IEnumerable<Phone> Phones { get { return phones; } }
public virtual IEnumerable<Postal> Postals { get { return postals; } }
}
那么,我是否可以从四个联系人表(父级和子级)下降到三个课程?并且,如何将这三个类映射到 UserContact 表。我假设我可以有三个 IList,每个类一个。
How do I map these existing tables to the classes below?
I have the following tables:
CREATE TABLE dbo.UserContact (
UserContactId int NOT NULL IDENTITY (1, 1),
UserId int NOT NULL,
ContactId int NOT NULL,
UserContactTypeId int NOT NULL,
FromDt datetime NULL,
ThruDt datetime NULL,
CreateDt datetime NOT NULL,
UpdateDt datetime NULL,
IsDeleted bit NULL,
CanSolicit bit NOT NULL
)
CREATE TABLE dbo.Contact (
ContactId int NOT NULL IDENTITY (1, 1),
CreateDt datetime NOT NULL,
UpdateDt datetime NULL
)
CREATE TABLE dbo.Electronic (
ContactId int NOT NULL,
URL nvarchar(512) NOT NULL,
ElectronicType smallint NULL
)
CREATE TABLE dbo.Phone (
ContactId int NOT NULL,
AreaCode nchar(3) NOT NULL,
PhoneNb nchar(7) NOT NULL,
Extension nchar(6) NULL,
PhoneType smallint NULL
)
CREATE TABLE dbo.Postal
(
ContactId int NOT NULL,
Street nvarchar(256) NOT NULL,
Specifier nvarchar(256) NULL,
GeocodeId int NULL
)
The tables Electronic, Phone and Postal are in a one-to-one relationship with Contact. The table UserContact is in a many-to-one with Contact; UserContact is an association table between User and Contact.
I also have the following classes:
public class Electronic : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Url { get; set; }
public ElectronicType Type { get; set; }
}
public class Postal : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string Street { get; set; }
public string Specifier { get; set; }
public Geocode Geocode { get; set; }
}
public class Phone : IntegerKeyEntity
{
public virtual ContactId { get; set; }
public virtual DateTime CreateDt { get; set; }
public virtual DateTime? UpdateDt { get; set; }
public string AreaCode { get; set; }
public string PhoneNb { get; set; }
public string Extension { get; set; }
public PhoneType Type { get; set; }
}
public class UserContact : IntegerKeyEntity
{
private ICollection<Electronic> electronics = new HashSet<Electronic>();
private ICollection<Phone> phones = new HashSet<Phone>();
private ICollection<Postal> postals = new HashSet<Postal>();
// props
public virtual IEnumerable<Electronic> Electronics { get { return electronics; } }
public virtual IEnumerable<Phone> Phones { get { return phones; } }
public virtual IEnumerable<Postal> Postals { get { return postals; } }
}
So, I do I get from the four Contact tables (parent and child) down to the three classes? And, how do I map those three classes to the UserContact table. I'm assuming I can have three ILists, one for each class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的建模不正确。在我看来,电子、电话和邮政扩展(继承)联系人,这应该在您的域模型中表达。这些类与 Contact 不是一一对应的,它们是扩展抽象 Contact 类型的具体类型。如果您以这种方式建模,则可以使用每个子类一个表的继承映射来映射联系人。
然后,用户将与联系人建立多对多关系,并且用户的联系人集合将包含任何类型的联系人。
就我个人而言,我会将所有联系人类型放入一个表中,并使用更简单的表/类映射。
I think you're modeling this incorrectly. It appears to me that Electronic, Phone, and Postal extend (inherit from) Contact and this should be expressed in your domain model. Those classes are not related to Contact by one-to-one, they are concrete types that extend the abstract Contact type. If you model it this way you can map Contact using table-per-subclass inheritance mapping.
User would then have a many-to-many-relationship with Contact, and the user's Contacts collection would contain Contacts of any type.
Personally I would put all the Contact types into one table and use the simpler table-per-class mapping.