0

I have entity like below

public abstract class MyBaseClass
{
    public int Id { get; set; }
}

public class MyConcrete : MyBaseClass
{
    public int TemplateName { get; set; }
    public int Total { get; set; }
}

public class MyOtherConcrete : MyBaseClass
{
    public int TemplateName { get; set; }
    public int MyProperty { get; set; }
    public string OtherProperty { get; set; }
}

using default initialization, EF will make table with columns like bellow:

Id
TemplateName
TemplateName1 // <-- this is the problem
Total
MyPorperty
MyOtherProperty

now my question how to configure EF so all the TemplateName property on derived class automatically mapped into TemplateName column without making another column. is it possible to configure it on OnModelCreating method?

EDIT

actually above was simplified version of my problem. i have 10 more entities some property might duplicated everywhere and i don't want to add any abstraction layers.

i have tried manually map the column on the OnModelCreating but having "Each property name in a type must be unique. Property name 'TemplateName' was already defined" exception any idea?

EDIT 2

so, i found here, that said it is impossible to do such thing like above in EF, it is weird for me..

Community
  • 1
  • 1
bonjorno
  • 201
  • 2
  • 12

2 Answers2

4

Move TemplateName into MyBaseClass to avoid this problem.

If necessary, you can use intermediate base classes to hold properties shared by only a subset of your classes.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • "i don't want to add any abstraction layers" = "I want to duplicate code." It is poor object design to duplicate properties that have identical meaning in multiple sibling subclasses. There is no down side to doing it properly (other than the one-time refactoring effort). If yo do not refactor to a proper design, you will continue to run into issues like the one you are having now. Note that intermediate base classes are not "abstraction layers", they are a means to organize the code. – Eric J. Sep 11 '12 at 16:24
  • yes i agree with that, but in higher context (bigger/complex object), but in my problem this is only an entity (value object), having 3 levels inheritance in entity is too much. and if i separate it into base classes than that will be a base class only contains one property, for me entity should be as simple as possible. thanks for the answer anyway ;) – bonjorno Sep 12 '12 at 03:23
  • @bonjorno: I totally disagree. You are making your "simple" project complex by refusing to follow an appropriate design pattern. Proof: You could have resolved this EF issue yesterday by making a 5 minute change to your class structure. Instead, you are still struggling with the issue. – Eric J. Sep 12 '12 at 14:33
0

After searching through the net, so far that was not possible to do that. since i realize that my inheritance tree is wrong.

so in my case, i should change my code to match the EF requirement, it is sound weird.. because in many case ENTITY is a NO NO to change, we usually create an entity that used in multiple project. event we found our entity wrong we won't update it because updating it will require massive change on the other projects.

so far, i think there is no exact answer for my question. will update it soon after EF support it.

bonjorno
  • 201
  • 2
  • 12