【问题标题】:EntityFramework Unable to determine composite primary key orderingEntityFramework 无法确定复合主键顺序
【发布时间】: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


    【解决方案1】:

    不确定如何重现您的迁移问题。 但是我确实在单元测试中重新创建了您的模型,并且在生成数据库时遇到了问题。
    无法确定类型 “UnitTestProject1.Portfolio”的复合主键排序。使用 ColumnAttribute(参见http://go.microsoft.com/fwlink/?LinkId=386388)...

    所以我添加了“列”属性,模型生成良好。
    [列(顺序 = 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; }
        }
    }
    

    【讨论】:

    • 啊,太好了,我确实尝试过订购,但我的做法略有不同,让我试一试。
    • 好的,为了节省时间,我现在首先使用数据库,并且必须先重新处理实体框架代码,也许还要上一门复数课程。我遇到了很多难以追查的问题和错误。
    猜你喜欢
    • 2017-08-07
    • 2016-01-20
    • 2015-12-17
    • 2017-02-26
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多