简体   繁体   中英

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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