1

Say I have a very simple class structure, more for convenience than anything.

class A{
    [Column("someValue")]
    public int someValue{get; set;}
}

[Table("tableB")]
class B{
    [Column("somethingElse")]
    public int somethingElse{get; set;}
    public A somethingEncapsulated{get; set;}
}

[Table("tableC")]
class C{
    [Column("otherSomething")]
    public int otherSomething{get; set;}
    public A somethingEncapsulated{get; set;}
}

And the database structure, which cannot be changed looks so

tableB
------
somethingElse int
someValue int

tableC
-----
otherSomething int
someValue int

Is it possible to represent this structure in Entity Framework without eliminating class A? If so, how?

Eranga
  • 32,181
  • 5
  • 97
  • 96
MushinNoShin
  • 4,695
  • 2
  • 32
  • 46
  • What do you mean by `without eliminating class A`? – Jayantha Lal Sirisena Jan 30 '12 at 03:52
  • 1
    Other alternative would be inheriting class `A` in class `B` and `C`. Then you can have several mapping options like `Table per hierarchy`, `table per type`. http://vincentlauzon.wordpress.com/2011/04/19/entity-framework-4-1-inheritance-7/ – Jayantha Lal Sirisena Jan 30 '12 at 04:02
  • @Jayantha, inheritance isn't an approach I thought of. It may work but the first concern that springs to mind is the appropriateness of saying "B is a A". What if B is already inheriting from another class, more appropriate class? – MushinNoShin Jan 30 '12 at 05:13

1 Answers1

3

You can use complex type mapping in this scenario.

[ComplextType]
public class A{
    [Column("someValue")]
    public int someValue{get; set;}
}

Go through this article

Eranga
  • 32,181
  • 5
  • 97
  • 96