开发者

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

开发者 https://www.devze.com 2023-03-22 19:11 出处:网络
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 + TableKe

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(开发者_如何学JAVAy => 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?


KeyType.... are you sure, that type of key will uniquely identify you key? I would go with names and reflection perhaps.

0

精彩评论

暂无评论...
验证码 换一张
取 消