Per
Per

Reputation: 1425

Entity Framework 4.1: Using TPH and mapping properties to existing database columns

I got this simple class hierarchy;

public class A 
{  
 [Key]
 public int Id { get; set; }
}  

public class B : A 
{
 public string Name { get; set; }
}

public class C : A 
{
 public string Name { get; set; }
}

Using TPH this will end upp with a table looking something like this;

Table A, fields Id, Name, Name1, Discriminator.

I want class B and C name property to map to the same field, ie Name, using these mappings.

Property(x => x.Id)
  .HasColumnName("Id");

Map<B>(m =>
{
    m.Properties(p => new
    {
        Name = p.Name,
    });
});

Map<C>(m =>
{
    m.Properties(p => new
    {
        Name = p.Name,
    });
});

How can I make sure to map to the same column for my subtypes (B and C) property Name? Is there a HasColumnName to be used with the Properties collection?

Upvotes: 1

Views: 661

Answers (1)

Alexandre Brisebois
Alexandre Brisebois

Reputation: 6743

THP does not allow for you to share properties between the objects unless the property is on the base class. Using TPH, a property is either Shared between all implementations or belongs to one of the specialized implementations.

Place Name property on the base class and this should solve your problem.

Upvotes: 2

Related Questions