为什么 EF 在保存具有相关对象列表(1 到多个)的对象时尝试将 List<Type> 转换为 Type?
Why EF tries to convert List<Type> to Type when saving an object with a list of related objects (1 to many)?
当我尝试通过调用 context.Consumption.Add(myConsumption)
然后 context.SaveChanges()
来保存 Consumption 对象时,我得到一个 System.InvalidCastException。我不知道为什么 EF 要将 List<Payment>
转换为 Payment
,因为 Consumption 的 Payments 属性 是 List<Payment>
.
类型
public class Consumption {
public int Id { get; set; }
...
public virtual List<Payment> Payments { get; set; }
}
public class Payment {
...
[ForeignKey("Id")]
public Consumption Consumption { get; set; }
}
我觉得你有点不对劲
假设您有以下设计
public class Consumption
{
public int Id { get; set; }
...
public virtual List<Payment> Payments { get; set; }
}
public class Payment {
public int Id { get; set; }
public Consumption Consumption { get; set; }
public int ConsumptionId { get; set; }
}
EF 足够聪明,可以根据命名约定找出关系和数据库脚手架
但是,如果您需要使用注释,则需要决定如何向 EF 告知您的关系
public class Payment {
public int Id { get; set; }
public Consumption Consumption { get; set; }
[ForeignKey("Consumption")] // point to the FK to the navigation property
public int ConsumptionId { get; set; }
}
或
public class Payment {
public int Id { get; set; }
[ForeignKey("ConsumptionId")] // point to the navigation property to the FK
public Consumption Consumption { get; set; }
public int ConsumptionId { get; set; }
}
或
public class Consumption
{
public int Id { get; set; }
...
[ForeignKey("ConsumptionId")] // Point to the dependent FK
public virtual List<Payment> Payments { get; set; }
}
我怀疑在你的数据库中(这应该很容易检查)你的 Consumption
table 中只有一个单数 foreign key
for payment
您似乎指向付款 table 的 Primary key
。不管怎样,让我感到惊讶的是 EF 甚至允许您进行此迁移(如果我没看错的话)
当我尝试通过调用 context.Consumption.Add(myConsumption)
然后 context.SaveChanges()
来保存 Consumption 对象时,我得到一个 System.InvalidCastException。我不知道为什么 EF 要将 List<Payment>
转换为 Payment
,因为 Consumption 的 Payments 属性 是 List<Payment>
.
public class Consumption {
public int Id { get; set; }
...
public virtual List<Payment> Payments { get; set; }
}
public class Payment {
...
[ForeignKey("Id")]
public Consumption Consumption { get; set; }
}
我觉得你有点不对劲
假设您有以下设计
public class Consumption
{
public int Id { get; set; }
...
public virtual List<Payment> Payments { get; set; }
}
public class Payment {
public int Id { get; set; }
public Consumption Consumption { get; set; }
public int ConsumptionId { get; set; }
}
EF 足够聪明,可以根据命名约定找出关系和数据库脚手架
但是,如果您需要使用注释,则需要决定如何向 EF 告知您的关系
public class Payment {
public int Id { get; set; }
public Consumption Consumption { get; set; }
[ForeignKey("Consumption")] // point to the FK to the navigation property
public int ConsumptionId { get; set; }
}
或
public class Payment {
public int Id { get; set; }
[ForeignKey("ConsumptionId")] // point to the navigation property to the FK
public Consumption Consumption { get; set; }
public int ConsumptionId { get; set; }
}
或
public class Consumption
{
public int Id { get; set; }
...
[ForeignKey("ConsumptionId")] // Point to the dependent FK
public virtual List<Payment> Payments { get; set; }
}
我怀疑在你的数据库中(这应该很容易检查)你的 Consumption
table 中只有一个单数 foreign key
for payment
您似乎指向付款 table 的 Primary key
。不管怎样,让我感到惊讶的是 EF 甚至允许您进行此迁移(如果我没看错的话)