Class and Interface hierarchies in Entity Framework?

后端 未结 1 986
清酒与你
清酒与你 2021-02-04 11:15

I have two related classes which share a common interface and are both stored in the same underlying database table. However, the Entity Framework generates one common class, wh

1条回答
  •  故里飘歌
    2021-02-04 12:12

    Is this data discriminated? i.e. does AccountType define which type it is? If so:

    • EF should create the Account entity from the storage
    • you then create 2 subclasses (UserAccount and GroupAccount)
    • in the mapping for Account, specify a predicate "add a condition"
      • have it map to UserAccount where the AccountType (storage) field is 1 (or whatever)
      • have it map to GroupAccount where the AccountType (storage) field is 2 (or whatever)

    The account type then should completely disappear from the Account object (unmap it if not). To get just the UserAccount records, you use

     .Accounts.OfType()...
    

    The Account class should probably be abstract in this model. The interface stuff can be added via a partial class - i.e. in a separate file, define:

    partial class Account : IAccount {
       // extra code here
    }
    

    etc

    A reasonable walkthrough is here.

    0 讨论(0)
提交回复
热议问题