代码之家  ›  专栏  ›  技术社区  ›  alphadogg

如何使用Fluent NHibernate将一对一的父子表折叠成类?

  •  -1
  • alphadogg  · 技术社区  · 14 年前

    如何将这些现有表映射到下面的类?

    我有以下表格:

    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
    )
    

    电子、电话和邮政表格与联系人是一对一的关系。User Contact表是一个多对一的Contact表;UserContact是用户和联系人之间的关联表。

    我还有以下课程:

    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,每节课一个。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Jamie Ide    14 年前

    我认为你的模型不正确。在我看来,电子、电话和邮政扩展(继承自)联系人,这应该在您的域模型中表示。这些类不是一对一地与Contact相关,而是扩展抽象Contact类型的具体类型。如果你用这种方式建模,你可以用 table-per-subclass inheritance mapping .

    然后,用户将与联系人建立多对多关系,并且用户的联系人集合将包含任何类型的联系人。

    就我个人而言,我会将所有联系人类型放在一个表中,并使用更简单的表/类映射。