无法确定关系的主体端

Unable to determine the principal end of the relationship

我正在使用 VS2015 EF6 Code First。我使用强大的工具生成了一个 read-only 模型,这样我就可以看到 EF 如何解释我的 POCO

它想出了这个,我觉得还不错:

我的问题是当我 运行 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 如下:请注意,我必须将导航道具注释掉回到视图,否则我会收到循环引用错误:

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' 关系的主体端。多个添加的实体可能具有相同的主键。

有人看到我哪里错了吗?

问题是由于我在列 table 中将 ViewId 作为视图 table 的外键引起的。当我删除它并改为使用 public 的导航 属性 virtual View View { get;放; } 然后它工作正常。

这是新模型:

进行此更改后,我不再收到错误 "Unable to determine the principal end of the relationship" 但是,我确实收到有关无法反序列化 JSON 的错误到循环错误。我能够解决从视图 [=29= 中删除显式导航 属性 (public 虚拟列表列 { get; set;}) ]