【问题标题】:Implementing Fluent API kind of method in C# for ef Code-First在 C# 中为 ef Code-First 实现 Fluent API 类型的方法
【发布时间】:2011-07-22 14:24:41
【问题描述】:

我首先使用 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>
{...

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

【问题讨论】:

    标签: c# linq linq-to-entities entity-framework-4.1 expression-trees


    【解决方案1】:

    KeyType....你确定,那种类型的钥匙会唯一地识别你的钥匙吗?也许我会选择名字和反思。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2013-10-21
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多