EntityFramework无法确定复合主键排序
本文关键字:排序 复合 无法确定 EntityFramework | 更新日期: 2023-09-27 18:20:20
我尝试使用EF代码firts来创建一个系统,在这个系统中,您有一个团队有一个想法,但该团队也可以有属于其他团队的想法实体组合。我在添加迁移和使关系正常工作方面遇到了困难。这是型号的代码:
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; }
}
团队和创意是1:1,这种关系很好。我认为问题是,我的投资组合中没有Id字段,因为它与团队1:1,只有团队Id。
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'.
不确定如何重现您的迁移问题。但我确实在单元测试中重新创建了您的模型,并且在生成数据库时遇到了问题
无法确定类型"UnitTestProject1.Portfolio"的组合主键顺序。请使用ColumnAttribute(请参阅http://go.microsoft.com/fwlink/?LinkId=386388)
所以我添加了"Column"属性,模型生成得很好
[列(顺序=n)]
也许这可以帮助您修复迁移
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
using (var context = new TestDbContext())
{
var teams = context.Teams.ToList();
var ideas = context.Ideas.ToList();
var portfolios = context.Portfolios.ToList();
}
}
}
public class TestDbContext : DbContext
{
public DbSet<Team> Teams { get; set; }
public DbSet<Idea> Ideas { get; set; }
public DbSet<Portfolio> Portfolios { get; set; }
}
public class Portfolio
{
[Key]
[Column(Order = 1)]
public int Id { get; set; }
public string Name { get; set; }
[Key, ForeignKey("Idea")]
[Column(Order = 2)]
public int IdeaId { get; set; }
public virtual Idea Idea { get; set; }
[Key, ForeignKey("Team")]
[Column(Order = 3)]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
public class Idea
{
public int Id { get; set; }
public string Name { get; set; }
[Key, ForeignKey("Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
}
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Portfolio> Portfolios { get; set; }
[Key, ForeignKey("Idea")]
public int IdeaId { get; set; }
public virtual Idea Idea { get; set; }
}
}