gpt4 book ai didi

c# - 在 Entity Framework Code First 中执行启用迁移时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:41 25 4
gpt4 key购买 nike

我正在尝试在 Person 和 Car 之间创建 1:many 关系。我知道这是微不足道的。但是在包管理器控制台中传递 Enable-Migrations 时,我得到了这个:

Car_Person_Target_Car_Person_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'PersonID' on entity 'Car' does not match the type of property 'FirstName' on entity 'Person' in the referential constraint 'Car_Person'.

Car_Person_Target_Car_Person_Source: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'PersonSsn' on entity 'Car' does not match the type of property 'LastName' on entity 'Person' in the referential constraint 'Car_Person'.

人类:

public class Person
{
public Person()
{
Cars = new List<Car>();
}
[Key, Column(Order = 0)]
public int ID { get; set; }
[Key, Column(Order = 1)]
public int Ssn { get; set; }
public int Age { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

public virtual ICollection<Car> Cars { get; set; }
}

汽车类:

public class Car
{
[Key, Column(Order = 0)]
public int ID { get; set; }
[Key, Column(Order = 1)]
public int ChassisSerial { get; set; }

[ForeignKey("Person"), Column(Order = 0)]
public int? PersonID { get; set; }
[ForeignKey("Person"), Column(Order = 1)]
public int? PersonSsn { get; set; }

public virtual Person Person { get; set; }
}

提前致谢。

最佳答案

将外键的类型改为“int”,必须与Person的PK类型匹配

public class Car
{
[Key, Column(Order = 0)]
public int ID { get; set; }
[Key, Column(Order = 1)]
public int ChassisSerial { get; set; }

[ForeignKey("Person"), Column(Order = 0)]
public int PersonID { get; set; }
[ForeignKey("Person"), Column(Order = 1)]
public int PersonSsn { get; set; }

public virtual Person Person { get; set; }
}

关于c# - 在 Entity Framework Code First 中执行启用迁移时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34530456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com