简体   繁体   English

在实体框架中用不同的名称两次映射外键

[英]Mapping Foreign Key twice with different names in Entity Framework

I have a MasterConfig table, where I want to store my values in CodeValue format. 我有一个MasterConfig表,我想以CodeValue格式存储我的值。 ie

ID   CODE            VALUE
1.   BusinessType    IT
2.   BusinessType    Marketing
3.   ContractType    General
4.   ContractType    Custom

So, to get records based on CODE field. 因此,要获取基于CODE字段的记录。 eg. 例如。 getCodeValue/ContractType getCodeValue /合约类型

Now, I have a Contract table where I have to map these fields as Foreign Keys. 现在,我有了一个Contract表,在其中必须将这些字段映射为外键。 My contract model looks like: 我的合同模型如下所示:

public class Contract
{
   [Key]
   public int ID{get; set;}
   public string Name{get; set;}

   [ForeignKey("CodeValue")]
   public int BusinessTypeID{get; set;}

   [ForeignKey("CodeValue")]
   public int ContractTypeID{get; set;}

   public virtual CodeValue CodeValue {get; set;}
}

While creating Controller with view, I get error that "Unable to retrieve metadata for Contract Model. Not able to map the fields". 使用视图创建Controller时,出现错误“无法检索合约模型的元数据。无法映射字段”。

Please suggest me a solution for this Master Config relation in Entity Framework (ASP.NET MVC) ? 请为我建议一种针对实体框架(ASP.NET MVC)中的主配置关系的解决方案?

It needs to map against a navigation property in the Contract class, something like this: 它需要映射到Contract类中的导航属性,如下所示:

public class Contract
{
   [Key]
   public int ID { get; set; }
   public string Name { get; set; }

   [ForeignKey("BusinessType")]
   public int BusinessTypeID { get; set; }

   [ForeignKey("ContractType")]
   public int ContractTypeID { get; set; }

   public virtual CodeValue BusinessType { get; set; }
   public virtual CodeValue ContractType { get; set; }
}

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

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