fle*_*esh 11 c# entity-framework class-hierarchy
我有两个相关的类,它们共享一个公共接口,并且都存储在同一个底层数据库表中.但是,实体框架生成一个公共类,我真的需要两个不同的类.我该如何解决这个问题?最好使用基类而不是接口吗?如何更改EF模型以提供映射到一个表的两个类?
编辑: 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; }
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ell 14
这些数据是否有歧视?即AccountType定义它是哪种类型?如果是这样:
然后,帐户类型应完全从Account对象中消失(如果不是,则取消映射).要获取UserAccount记录,请使用
.Accounts.OfType<UserAccount>()...
Run Code Online (Sandbox Code Playgroud)
在此模型中,Account类可能应该是抽象的.接口内容可以通过部分类添加 - 即在单独的文件中,定义:
partial class Account : IAccount {
// extra code here
}
Run Code Online (Sandbox Code Playgroud)
等等
这里有一个合理的演练.
| 归档时间: |
|
| 查看次数: |
5972 次 |
| 最近记录: |