简体   繁体   English

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

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

I am using EF 4.1 code first, and using fluent API for entity Configuration.我首先使用 EF 4.1 代码,并使用流利的 API 进行实体配置。

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.几乎我的数据库中的每个表键都是“ICustomerId + TableKey”的组合,因此,每个外键关系都需要它来引用它。

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

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?任何想法,我该如何实现这种方法?

KeyType.... are you sure, that type of key will uniquely identify you key? KeyType .... 你确定,那种类型的钥匙会唯一地识别你的钥匙吗? I would go with names and reflection perhaps.我可能会用名称和反射 go 。

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

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