Pergunta

Como faço para mapear essas tabelas existentes para as classes abaixo?

Eu tenho as seguintes tabelas:

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
)

As tabelas eletrônicas, telefone e postal estão em um relacionamento individual com o contato. A tabela UserContact está em muitos para um contato; UserContact é uma tabela de associação entre usuário e contato.

Eu também tenho as seguintes aulas:

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

Então, eu recebo das quatro tabelas de contato (pai e filho) até as três classes? E como faço para mapear essas três classes para a tabela UserContact. Suponho que eu possa ter três ilistas, um para cada classe.

Foi útil?

Solução

Eu acho que você está modelando isso incorretamente. Parece -me que o contato eletrônico, telefônico e postal (herdado do) contato e isso deve ser expresso no seu modelo de domínio. Essas classes não estão relacionadas ao contato com um a um, são tipos de concreto que estendem o tipo de contato abstrato. Se você o modelar dessa maneira, pode mapear o contato usando o uso Mapeamento de herança da tabela por sub-subclasse.

O usuário teria um relacionamento com muitos para muitos com o contato e a coleção de contatos do usuário conteria contatos de qualquer tipo.

Pessoalmente, eu colocava todos os tipos de contato em uma tabela e usava o mapeamento mais simples de tabela por classe.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top