繁体   English   中英

在 C# 中为 ef Code-First 实现 Fluent API 类方法

[英]Implementing Fluent API kind of method in C# for ef Code-First

我首先使用 EF 4.1 代码,并使用流利的 API 进行实体配置。

我正在使用以下方式来配置我的实体。 几乎我的数据库中的每个表键都是“ICustomerId + TableKey”的组合,因此,每个外键关系都需要它来引用它。

  HasRequired(x => x.Company)
  .WithMany(y => y.CompanyContacts)
  .HasForeignKey(p => new { p.ICustomerID, p.CompanyID })
  .WillCascadeOnDelete(false);

我想在我的基础 class(继承自 EntityTypeConfiguration)中实现一个方法,该方法将采用 TargetEntity、TargetKey,并将执行上述外键创建(通过自动包含 ICustomerId。我的 Clas 定义:-

public class CompanyContactsMapping 
               : BaseInsightTypeConfiguration<CompanyContacts,int>
{...

public class BaseInsightTypeConfiguration<T, TKeyType> 
               : EntityTypeConfiguration<T> where T : BaseInsightEntity<TKeyType>
{...

任何想法,我该如何实现这种方法?

KeyType .... 你确定,那种类型的钥匙会唯一地识别你的钥匙吗? 我可能会用名称和反射 go 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM