-1

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.

alphadogg
  • 12,762
  • 9
  • 54
  • 88

1 Answers1

1

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.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • Correct. The three child tables extend the parent Contact table in the database. I guess I was trying to keep the middle layer simple by making the Contact table invisible and making the children independent classes. I may go back and do as you suggest, but assuming I don't change my mind, is the above possible? If so how? – alphadogg Nov 19 '10 at 14:27
  • What you're planning to do is much much more complicated than using inheritance, especially if you use table-per-class mapping. With inheritance the contact table would still be invisible except in your mapping. If you don't change your mind then I would suggest using views for the three contact types. – Jamie Ide Nov 19 '10 at 17:53
  • I've done it as you recommended, which was where I was going first. We'll see if I need to change it. No need to complicate things early. – alphadogg Nov 25 '10 at 18:42