【发布时间】:2015-11-03 16:26:22
【问题描述】:
我正在尝试使用 EF 代码创建一个系统,在该系统中,您的团队有一个想法,但该团队也可以拥有属于其他团队的想法实体组合。我在添加迁移和使关系正常工作时遇到了麻烦。这是模型的代码:
public class Team
{
public Team()
{
Portfolio = new HashSet<Portfolio>();
}
public int Id { get; set; }
public string Name { get; set; }
public decimal Balance { get; set; }
public virtual ICollection<Portfolio> Portfolio { get; set; }
public virtual Idea Idea { get; set; }
}
public class Idea
{
[Key, ForeignKey("Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public decimal CurrentValue { get; set; }
}
public class Portfolio
{
public int Id { get; set; }
public int IdeaId { get; set; }
public Idea Idea { get; set; }
public int TeamId { get; set; }
public Team Team { get; set; }
}
Team 和 Idea 是 1:1,这种关系很好。我认为问题在于我有与没有 Id 字段的想法相关的投资组合,因为它与 Team 是 1:1,它只有 teamid。
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Ideas_dbo.Teams_TeamId". The conflict occurred in database "VHolsDaqDb", table "dbo.Teams", column 'Id'.
【问题讨论】:
标签: c# entity-framework