C# EF一对一主关系错误

C# EF一对一主关系错误,c#,entity-framework,C#,Entity Framework,我已经研究了这里的其他问题,以及我的其他使用EF的项目,但无法找出为什么我在尝试使用EF创建带有视图的控制器时出错 我得到的信息是,它不理解公司和泳池之间的主要关系,这是一对一的关系 无法确定PoolCar和Company类型之间关联的主要端点。 公司1 1台球车1*CarAllocation 我已经在依赖表(PoolCar)上分配了外键,但它仍然抛出相同的错误 我错过了什么 [Table("Company")] public class Company { [K

我已经研究了这里的其他问题,以及我的其他使用EF的项目,但无法找出为什么我在尝试使用EF创建带有视图的控制器时出错

我得到的信息是,它不理解
公司
泳池
之间的主要关系,这是一对一的关系

无法确定PoolCar和Company类型之间关联的主要端点。

公司1 1台球车1*CarAllocation

我已经在依赖表(PoolCar)上分配了外键,但它仍然抛出相同的错误

我错过了什么

[Table("Company")]
    public class Company
    {
        [Key]
        public int companyId { get; set; }
        public string companyName { get; set; }
        // navigation property
        public virtual PoolCar poolCar { get; set; }
    }
    [Table("PoolCar")]
    public class PoolCar
    {
        [Key]
        public int poolCarId { get; set; }
        public int companyId { get; set; }
        [ForeignKey("companyId")]
        public Company company { get; set; }
        public string poolCarName { get; set; }
        // navigation property
        public virtual IList<CarAllocation> carAllocations { get; set; }
    }
    [Table("CarAllocation")]
    public class CarAllocation
    {
        [Key]
        public int carAllocationId { get; set; }
        public int poolCarId { get; set; }
        [ForeignKey("poolCarId")]
        public PoolCar poolCar { get; set; }
        public string allocationName { get; set; }
    }
[表格(“公司”)]
公营公司
{
[关键]
public int companyId{get;set;}
公共字符串companyName{get;set;}
//导航属性
公共虚拟池车池车{get;set;}
}
[表(“池车”)]
公车
{
[关键]
public int poolCarId{get;set;}
public int companyId{get;set;}
[外键(“公司ID”)]
上市公司{get;set;}
公共字符串poolCarName{get;set;}
//导航属性
公共虚拟IList配置{get;set;}
}
[表(“CarAllocation”)]
公共类分配
{
[关键]
public int carAllocationId{get;set;}
public int poolCarId{get;set;}
[ForeignKey(“poolCarId”)]
公共撞车撞车{get;set;}
公共字符串分配名称{get;set;}
}
解决方案1 这是外键和关联键独立的问题。所以你只要做一个流畅的映射,我们就能非常清楚地理解这个问题。在fluent Api部分中执行以下操作

modelBuilder.Entity<Company>()
        .HasOptional(obj => obj.poolCar)
        .WithRequired(obj1 => obj1.company);
modelBuilder.Entity()
.has可选(obj=>obj.poolCar)
.带必填项(obj1=>obj1.公司);
解决方案1 这是外键和关联键独立的问题。所以你只要做一个流畅的映射,我们就能非常清楚地理解这个问题。在fluent Api部分中执行以下操作

modelBuilder.Entity<Company>()
        .HasOptional(obj => obj.poolCar)
        .WithRequired(obj1 => obj1.company);
modelBuilder.Entity()
.has可选(obj=>obj.poolCar)
.带必填项(obj1=>obj1.公司);

我想我以前可能遇到过这个问题,我相信这可能是EF中的一个bug。显然,您可以通过在fluent API中配置关系来解决这个问题,请参见@vinodh answer,但如果您坚持使用数据注释,则可以将外键属性改为放在
poolCarId
属性上

[Table("PoolCar")]
public class PoolCar
{
    [Key, ForeignKey("company")]
    public int poolCarId { get; set; }
    public int companyId { get; set; }
    public Company company { get; set; }
    public string poolCarName { get; set; }
    // navigation property
    public virtual IList<CarAllocation> carAllocations { get; set; }
}   
[表(“池车”)]
公车
{
[钥匙,外国钥匙(“公司”)]
public int poolCarId{get;set;}
public int companyId{get;set;}
上市公司{get;set;}
公共字符串poolCarName{get;set;}
//导航属性
公共虚拟IList配置{get;set;}
}   
还值得注意的是,如果您在关系的主要方面没有导航属性,也就是说,如果您从
公司
模型中删除了
public-virtual-PoolCar-PoolCar{get;set;}
,那么您的代码就会起作用


我相信这是一个bug,因为当您显式声明外键时,EF没有理由不能区分哪一方是依赖方

我想我以前可能遇到过这个问题,我相信这可能是EF中的一个bug。显然,您可以通过在fluent API中配置关系来解决这个问题,请参见@vinodh answer,但如果您坚持使用数据注释,则可以将外键属性改为放在
poolCarId
属性上

[Table("PoolCar")]
public class PoolCar
{
    [Key, ForeignKey("company")]
    public int poolCarId { get; set; }
    public int companyId { get; set; }
    public Company company { get; set; }
    public string poolCarName { get; set; }
    // navigation property
    public virtual IList<CarAllocation> carAllocations { get; set; }
}   
[表(“池车”)]
公车
{
[钥匙,外国钥匙(“公司”)]
public int poolCarId{get;set;}
public int companyId{get;set;}
上市公司{get;set;}
公共字符串poolCarName{get;set;}
//导航属性
公共虚拟IList配置{get;set;}
}   
还值得注意的是,如果您在关系的主要方面没有导航属性,也就是说,如果您从
公司
模型中删除了
public-virtual-PoolCar-PoolCar{get;set;}
,那么您的代码就会起作用


我相信这是一个bug,因为当您显式声明外键时,EF没有理由不能区分哪一方是依赖方

试试这个,让我知道如果它不能解决,我会给出另一个解决方案。我会试试,让我看看Fluent Api,因为我从来没有使用过它,不知道把它放在哪里等等。它在您上下文中的Fluent Api部分下。您可以在
DbContext
中重写
OnModelCreating
。实际上,FluentApi用于验证我们的poco类。。我们有注释和fluentapi。如果使用此验证,则可以很好地处理此类错误。这就是为什么我要你这么做试试这个,让我知道如果它不能解决,我会给出另一个解决方案。我会试试,让我看看Fluent Api,因为我从来没有使用过它,不知道把它放在哪里等等。它在您上下文中的Fluent Api部分下。您可以在
DbContext
中重写
OnModelCreating
。实际上,FluentApi用于验证我们的poco类。。我们有注释和fluentapi。如果使用此验证,则可以很好地处理此类错误。这就是为什么我要你这么做很高兴不仅仅是我-这个bug还在最新的.NET中吗?我相信目前使用的是4.2。我在公司模型上尝试过延迟加载,但仍然会遇到同样的错误。以前没有尝试过使用Fluent Api。将检查它。这实际上与.NET版本无关,而是与EF版本有关。我们目前使用版本6,但问题仍然存在。我不是100%确定这是一个错误,他们可能是一个很好的理由。很高兴这不仅仅是我-这个错误仍然存在吗