Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
Entity framework 实体框架具有1:1关系的ForeignKey错误_Entity Framework_Asp.net Core 2.0 - Fatal编程技术网

Entity framework 实体框架具有1:1关系的ForeignKey错误

Entity framework 实体框架具有1:1关系的ForeignKey错误,entity-framework,asp.net-core-2.0,Entity Framework,Asp.net Core 2.0,在建立一对多关系时,我可以做到这一点,而且效果很好: EntityTypeBuilder<BrandOwner> brandOwner = X brandOwner.HasMany(a => a.Brands).WithOne(b => b.Owner).HasForeignKey("OwnerId"); 错误是: InvalidOperationException:您正在配置“Person”和“SocialInsuranceCard”之间的关系,但在“OwnerId

在建立一对多关系时,我可以做到这一点,而且效果很好:

EntityTypeBuilder<BrandOwner> brandOwner = X

brandOwner.HasMany(a => a.Brands).WithOne(b => b.Owner).HasForeignKey("OwnerId");
错误是: InvalidOperationException:您正在配置“Person”和“SocialInsuranceCard”之间的关系,但在“OwnerId”上指定了外键。必须在属于关系的类型上定义外键

示例类:

public class BrandOwner
{
    public int Id { get; set; }
    public virtual List<Brand> Brands { get; set; }
}

public class Brand
{
    public int Id { get; set; }
    public int OwnerId { get; set; } // I'm aware of the "BrandOwnerId" convention
    public virtual BrandOwner Owner { get; set; }
}


pubic class Person
{
    public int Id { get; set; }
    public virtual SocialInsuranceCard SinCard { get; set; }
}

pubic class SocialInsuranceCard
{
    public int Id { get; set; }
    public int OwnerId; // I know I could get auto mapping by using "PersonId"
    public virtual Person Owner { get; set; }
}
公共类BrandOwner
{
公共int Id{get;set;}
公共虚拟列表品牌{get;set;}
}
大众品牌
{
公共int Id{get;set;}
public int OwnerId{get;set;}//我知道“BrandOwnerId”约定
公共虚拟品牌所有者{get;set;}
}
公共阶层人士
{
公共int Id{get;set;}
公共虚拟社交卡SinCard{get;set;}
}
公共阶级社会保险卡
{
公共int Id{get;set;}
public int OwnerId;//我知道我可以通过使用“PersonId”获得自动映射
公共虚拟人所有者{get;set;}
}

为什么在1:1关系中使用非泛型HasForeignKey会出现错误?

您的结构不是1对1,而是1对多。1对1通常设置为共享相同的ID:

public class Car
{
    public int CarId { get; set; }
    public virtual Seat seat { get; set; }
}

public class Seat
{
    public int CarId { get; set; } // PK and FK to Car
    public virtual Car car { get; set; }
}
这假设一辆车有一个座位。如果你想在一辆车里找到一个可以有多个座位的主座椅,你可能需要:

public class Car
{
    public int CarId { get; set; }
    public virtual ICollection<Seat> Seats { get; set; }
    public virtual Seat PrimarySeat { get; set; }
}

public class Seat
{
    public int SeatId { get; set; } // PK
    public int CarId { get; set; } // FK to Car 1-to-Many
    public virtual Car car { get; set; }
}

设置此设置可能是可能的,但需要对所有汽车和座椅关系进行显式映射,而不是将其留给约定。Car将明确需要知道如何解析其座椅集合(使用座椅上的CarId)和PrimarySeat实例。(多对一)它不是一对一,特别是因为没有什么可以阻止多辆车指定同一个SeatID作为主SeatID。此外,也没有强制规定PrimarySeat属于汽车座椅系列。任何此类限制和规则都需要在应用程序级别强制执行,可能需要每天对数据库运行健康检查查询以报告任何不匹配。(带主座椅的汽车与carId不匹配)

我更改了样品。我真正的代码不是关于汽车座椅(或者关于人和罪恶卡)。忽略数据,不要试图解决不存在的数据库架构问题,只需阅读我的问题,并解释为什么列表与单个对象相比需要一般使用HasForeignKey。因为您的代码将在
Person
中找到
OwnerId
属性,而不是
SocialInsuranceCard
,然后出现错误,说明必须在属于关系的类型上定义外键。因为找不到。@XingZou但lamda指定SinCard.Owner right。。。?所以它知道查看SinCard类型的对象,因为lambda选择了它。
public class Car
{
    public int CarId { get; set; }
    public virtual Seat seat { get; set; }
}

public class Seat
{
    public int CarId { get; set; } // PK and FK to Car
    public virtual Car car { get; set; }
}
public class Car
{
    public int CarId { get; set; }
    public virtual ICollection<Seat> Seats { get; set; }
    public virtual Seat PrimarySeat { get; set; }
}

public class Seat
{
    public int SeatId { get; set; } // PK
    public int CarId { get; set; } // FK to Car 1-to-Many
    public virtual Car car { get; set; }
}
public class Car
{
    // ...
    public int PrimarySeatId { get; set; } // FK for Primary Seat
}