Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实体框架中使用不同名称映射两次外键_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

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

C# 在实体框架中使用不同名称映射两次外键,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个MasterConfig表,我想在其中以CodeValue格式存储我的值。i、 e ID CODE VALUE 1. BusinessType IT 2. BusinessType Marketing 3. ContractType General 4. ContractType Custom 所以,根据代码字段获取记录。例如,getCodeValue/ContractType 现在,我有了一个合同表,我必须将这些字段映

我有一个MasterConfig表,我想在其中以CodeValue格式存储我的值。i、 e

ID   CODE            VALUE
1.   BusinessType    IT
2.   BusinessType    Marketing
3.   ContractType    General
4.   ContractType    Custom
所以,根据代码字段获取记录。例如,getCodeValue/ContractType

现在,我有了一个合同表,我必须将这些字段映射为外键。我的合同模型如下所示:

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;}
}
在使用视图创建控制器时,我遇到错误“无法检索合同模型的元数据。无法映射字段”


请为实体框架(ASP.NET MVC)中的此主配置关系提供解决方案?

它需要映射到
合同
类中的导航属性,如下所示:

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; }
}

所以,我们在CodeValue上创建了单独的派生类,并使用这些类链接外键。