无法确定关系的主要结束

时间:2016-02-26 12:17:00

标签: entity-framework ef-code-first ef-migrations

我正在使用VS2015 EF6 Code First。我使用电动工具生成只读模型,以便我可以看到EF如何解释我的POCO

它提出了这个,对我来说没问题:

enter image description here

我的问题是当我运行update-database尝试播种数据时。以下是迁移代码:

public partial class First : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Column",
                c => new
                    {
                        ColumnId = c.Int(nullable: false, identity: true),
                        ViewId = c.Int(nullable: false),
                        Heading = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.ColumnId)
                .ForeignKey("dbo.View", t => t.ViewId, cascadeDelete: true)
                .Index(t => t.ViewId);

            CreateTable(
                "dbo.View",
                c => new
                    {
                        ViewId = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.ViewId);

            CreateTable(
                "dbo.User",
                c => new
                    {
                        UserId = c.Int(nullable: false, identity: true),
                        AdLogonDomain = c.String(maxLength: 50),
                        AdLogonId = c.String(maxLength: 50),
                    })
                .PrimaryKey(t => t.UserId);

            CreateTable(
                "dbo.UserViewColumn",
                c => new
                    {
                        UserViewColumnId = c.Int(nullable: false, identity: true),
                        Column_ColumnId = c.Int(),
                        User_UserId = c.Int(),
                        View_ViewId = c.Int(),
                    })
                .PrimaryKey(t => t.UserViewColumnId)
                .ForeignKey("dbo.Column", t => t.Column_ColumnId)
                .ForeignKey("dbo.User", t => t.User_UserId)
                .ForeignKey("dbo.View", t => t.View_ViewId)
                .Index(t => t.Column_ColumnId)
                .Index(t => t.User_UserId)
                .Index(t => t.View_ViewId);

        }

        public override void Down()
        {
            DropForeignKey("dbo.UserViewColumn", "View_ViewId", "dbo.View");
            DropForeignKey("dbo.UserViewColumn", "User_UserId", "dbo.User");
            DropForeignKey("dbo.UserViewColumn", "Column_ColumnId", "dbo.Column");
            DropForeignKey("dbo.Column", "ViewId", "dbo.View");
            DropIndex("dbo.UserViewColumn", new[] { "View_ViewId" });
            DropIndex("dbo.UserViewColumn", new[] { "User_UserId" });
            DropIndex("dbo.UserViewColumn", new[] { "Column_ColumnId" });
            DropIndex("dbo.Column", new[] { "ViewId" });
            DropTable("dbo.UserViewColumn");
            DropTable("dbo.User");
            DropTable("dbo.View");
            DropTable("dbo.Column");
        }
    }

这是我的种子方法:

protected override void Seed(TVS.ESB.BamPortal.DataLayer.UserPrefsContext context)
        {
            //  This method will be called after migrating to the latest version.

            context.UserRecs.AddOrUpdate(
              u => u.AdLogonId,
              new User { AdLogonDomain = "dom", AdLogonId = "logon" }
            );

            View erpView = new View()
            {
                Name = "erp"
            };

            View cceView = new View()
            {
                Name = "cce"
            };

            context.ViewRecs.AddOrUpdate(
              v => v.Name,
              erpView, cceView
            );

            context.ColumnRecs.AddOrUpdate(
              v => v.Heading,
              new Column { ViewId = erpView.ViewId, Heading = "ErpColumn1" },
              new Column { ViewId = erpView.ViewId, Heading = "ErpColumn2" },
              new Column { ViewId = cceView.ViewId, Heading = "CceColumn1" },
              new Column { ViewId = cceView.ViewId, Heading = "CceColumn2" }
            );

        }

专栏的POCO如下:注意,我必须将导航道具注释回View,否则我会得到一个循环引用错误:

public class Column
    {
        public int ColumnId { get; set; }
        public int ViewId { get; set; }
        [MaxLength(20)]
        public string Heading { get; set; }

        // with the following navigation 
        //prop I get the error: Circular reference detected exception while serializing object to JSON
        //public virtual View View { get; set; }
    }

在从PMC执行update-database并运行种子方法时,我收到以下错误:

无法确定'TVS.ESB.BamPortal.DataLayer.View_Columns'关系的主要结尾。多个添加的实体可能具有相同的主键。

任何人都知道我哪里出错了?

1 个答案:

答案 0 :(得分:0)

问题是由于我在列表中将ViewId作为View表的foregin键。当我删除它并改为使用公共虚拟视图的导航属性{get;组; 然后它工作正常。

这是新模型:

enter image description here

我做了这个更改后,我不再收到错误“无法确定关系的主要结束”但是,我确实收到一个错误,因为通知无法反序列化JSON错误。我能够解决从View表中删除列的显式Navigation属性(公共虚拟列表列{get; set;})