在 C# 中为 ef Code-First 实现 Fluent API 类型的方法
我首先使用 EF 4.1 代码,并使用 Fluent API 进行实体配置。
我正在使用以下方法来配置我的实体。我的数据库中几乎每个表键都是由“ICustomerId + TableKey”组成的,因此,每个外键关系也需要它引用它。
HasRequired(x => x.Company)
.WithMany(y => y.CompanyContacts)
.HasForeignKey(p => new { p.ICustomerID, p.CompanyID })
.WillCascadeOnDelete(false);
我想在我的基类(继承自 EntityTypeConfiguration)中实现一个方法,该方法将采用 TargetEntity、TargetKey,并将执行上述外键创建(通过自动包含 ICustomerId)。 我的类定义:-
public class CompanyContactsMapping
: BaseInsightTypeConfiguration<CompanyContacts,int>
{...
public class BaseInsightTypeConfiguration<T, TKeyType>
: EntityTypeConfiguration<T> where T : BaseInsightEntity<TKeyType>
{...
任何想法,我怎样才能实现这样的方法?
I am using EF 4.1 code first, and using fluent API for entity Configuration.
I am using following way to configure my entities. Almost every table key in my db is composite of "ICustomerId + TableKey", hence, every foreign key relationship requires it to refer it also.
HasRequired(x => x.Company)
.WithMany(y => y.CompanyContacts)
.HasForeignKey(p => new { p.ICustomerID, p.CompanyID })
.WillCascadeOnDelete(false);
I want to implement a method in my base class (which inherits from EntityTypeConfiguration), which will take TargetEntity, TargetKey, and will perform above foreign key creation (by including ICustomerId automatically.
My Clas Defintions:-
public class CompanyContactsMapping
: BaseInsightTypeConfiguration<CompanyContacts,int>
{...
public class BaseInsightTypeConfiguration<T, TKeyType>
: EntityTypeConfiguration<T> where T : BaseInsightEntity<TKeyType>
{...
Any Idea, how can I implement such method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
KeyType...您确定该类型的密钥将唯一标识您的密钥吗?也许我会选择名字和反思。
KeyType.... are you sure, that type of key will uniquely identify you key? I would go with names and reflection perhaps.