【问题标题】:Unable to determine the principal end of the relationship无法确定关系的主体端
【发布时间】:2016-02-26 12:17:00
【问题描述】:

我使用的是 VS2015 EF6 Code First。我已经使用电动工具生成了一个只读模型,以便我可以看到 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" }
            );

        }

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

有人知道我哪里出错了吗?

【问题讨论】:

  • 看起来仍然是圆形的。字面上地。你能消除 UserViewColumn 到 Column 的关系(或视图到列)吗?
  • 逆向工程会产生一些流畅的代码。那看起来像什么?

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


【解决方案1】:

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

这是新模型:

进行此更改后,我不再收到错误“无法确定关系的主体结束”但是,由于循环,我确实收到了有关无法反序列化 JSON 的错误错误。我能够解决从视图表中删除列的显式导航属性 (public virtual List Columns { get; set;})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 2013-05-07
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-06-14
    相关资源
    最近更新 更多