1

我有一个使用代码优先 EF 4.3 的基本模型的 DLL。我想要的是使用附加字段扩展该 dll 中的某些模型。

例如在 BaseModel.DLL

namespace BaseModel
{
   public class Account
   {
       public Id { get;set;}
       public string Name {get;set;}
   }
}

在一个引用项目中,我想扩展 Account 模型(和 DB 表):

public class Account : BaseModel.Account
{
    public string SomeAdditionalInfo { get;set;}
}

我想最终得到一个带有字段的表帐户

Id
Name
SomeAdditionalInfo

这样我就可以在几个类似的项目中继续重用 BaseModel(和逻辑)。我想我不能使用部分类,因为我们说的是不同的 DLL。也许是继承?我尝试了几种方法,但我不断遇到关于拥有 2 个具有相同名称的模型的冲突。

有什么提示吗?提示?解决方案?

4

1 回答 1

2

您可以通过 Table per Hierarchy 使用继承。您可以创建基类 AccountBase 和子类 Account:AccountBase:

public class AccountBase
{
   public Id { get;set;}
   public string Name {get;set;}
}
public class Account : AccountBase
{
    public string SomeAdditionalInfo { get;set;}
}

它生成表 AccountBase 将包含列 Id、Name、SomeAdditionalInfo 还有列鉴别器将包含该行中包含的类的实例。

于 2012-11-23T19:02:27.677 回答