代码之家  ›  专栏  ›  技术社区  ›  flesh

实体框架中的类和接口层次结构?

  •  11
  • flesh  · 技术社区  · 17 年前

    编辑: AccountType属性确定类的类型;用户或组。

    一些简单的代码:

    public interface IAccount
    {
        string Name { get; set; }
        AccountType AccountType { get; set; }
    }
    
    public class GroupAccount : IAccount
    {
        public string Name { get; set; }
        public GroupType GroupType { get; set; }
        public AccountType AccountType { get; set; }
    }
    
    public class UserAccount : IAccount
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public AccountType AccountType { get; set; }
    }
    
    1 回复  |  直到 17 年前
        1
  •  14
  •   Marc Gravell    17 年前

    这些数据有区别吗?i、 e.AccountType是否定义了它是哪种类型?如果是:

    • EF应该从存储中创建帐户实体
    • 然后创建2个子类(UserAccount和GroupAccount)
    • 在Account的映射中,指定谓词“添加条件”
      • 将其映射到UserAccount,其中AccountType(存储)字段为1(或其他值)
      • 将其映射到AccountType(存储)字段为2(或任意值)的GroupAccount

    然后,帐户类型应该从account对象中完全消失(如果没有,则取消映射)。要仅获取UserAccount记录,可以使用

     .Accounts.OfType<UserAccount>()...
    

    在这个模型中,Account类可能是抽象的。可以通过分部类添加接口内容-即在单独的文件中,定义:

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

    合理的演练是必要的 here .

    推荐文章