1

We are using Entity Framework 6.1 in our MVC application, using code-first and table-per-hierarchy approach since we are developing from scratch and we think this way is suitable for fast development. But now I have a question: say I have one base class and three sub classes, which look like this:

public class BaseClass
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class SubClassOne : BaseClass
{
    public double Volume { get; set; }        
}

public class SubClassTwo : BaseClass
{
    public double Volume { get; set; }
}

public class SubClassThree : BaseClass
{
    public int Number { get; set; }
}

When using default settings, EF will create 6 columns for the database table: ID, Name, Volume, Volume1, Number and Discriminater. The Volume column will only be used for SubClassOne and Volume1 will for SubClassTwo, Number only for SubClassThree.

I don't care too much about the "waste" of database row spaces. But here is one thing that's bothering me: if I am a new developer and is now looking at the database directly, the names of "Volume" and "Volume1" confuse me. I know on the code side there is SubClassOne and SubClassTwo and both have a property named "Volume". But I could never know which column is for which class. So my question is, is there any way to instruct Entity Framework (e.g., using attribute) that both SubClassOne.Volume and SubClassTwo.Volume are mapped to Volume column in the database. (another benefit is I reduce one column but that's not my main target).

I guess if I don't use Entity Framework, code-first, or table-per-hierarchy, and design the tables and especially the DAL myself, I can have full control and achieve this. But how about now?

tete
  • 4,859
  • 11
  • 50
  • 81
  • do you want to have a BaseClass table in DB? – MichaelMao Nov 19 '15 at 08:23
  • @MicahelMao Yes since I am using table-per-hierarchy and `BaseClass` is the base class, my table will be named BaseClasses and it contains records for both `BaseClass` and derived classes objects – tete Nov 19 '15 at 08:27
  • Have you tried this public abstract class BaseClass – MichaelMao Nov 19 '15 at 08:44
  • You can use Attribute [Column("Volume1")] and [Column("Volume2")] to both Class, so it can map to related Database Column and You have same Property name "Volume" – Parth Trivedi Nov 19 '15 at 08:47
  • @ParthTrivedi I tested it and it worked! I might need to do some more test but at least I was able to load property values for both classes under the same column "Volume". Thank you! – tete Nov 19 '15 at 09:53

1 Answers1

1

You can use Attribute [Column("Volume1")] and [Column("Volume2")] to both Class, so it can map to related Database Column and You have same Property name "Volume".

public class BaseClass
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class SubClassOne : BaseClass
{
    [Column("Volume1")]   
    public double Volume { get; set; }        
}

public class SubClassTwo : BaseClass
{
    [Column("Volume2")] 
    public double Volume { get; set; }
}

public class SubClassThree : BaseClass
{
    public int Number { get; set; }
}

This works fine.If any doubt then tell me.

Parth Trivedi
  • 3,802
  • 1
  • 20
  • 40
  • I think you misunderstood what I ask (and I made a mistake in the original post), although your answer points to the correct direction. My original problem is, if I use default setting, EF will generate a column called "Volume" for `SubClassOne.Volume` and another column called "Volume1" for `SubClassTwo`. I find it confusing since I won't be able to insert a row directly manually since I don't know which column is for which class property. So what I want is, since both properties have the same name, I want only one column in the database and EF would query this column for both properties – tete Nov 19 '15 at 10:43
  • And your suggestion of using `ColumnAttribute` definitely works here. Just need to use `[Column("Volume")]`. – tete Nov 19 '15 at 10:45