EF6代码从一到零,或者具有预定义的FK属性的一个

时间:2018-11-02 14:00:18

标签: c# asp.net-mvc entity-framework-6 ef-code-first ef-migrations

我正在处理一些旧代码,将向数据库添加新表 它将有两个一对零或一个关系,我需要“出于某种原因”将新表中的ForeignKeys定义为properties和The code:

public class EconomyInfo : Entity
{
     /*other props*/
    [ForeignKey("LogoImage")]
    [Required]
    public int LogoImage_Id { get; set; }
    public virtual Image LogoImage { get; set; }

    [ForeignKey("Organization")]
    [Required]
    public int Organization_Id { get; set; }
    public virtual Organization Organization { get; set; }

}

public class Image : Entity
{
    /*other props*/
    public virtual EconomyInfo Economyinfo { get; set; }
}

public class Organization : Entity
{
  /*other props*/
    public virtual EconomyInfo EconomyInfo { get; set; }

}

public class Entity
    {
        public int Id { get; set; }
    }

类似于此EF不会生成迁移并给出错误

  

EconomyInfo_LogoImage_Source ::多重性在关系“ EconomyInfo_LogoImage”中的角色“ EconomyInfo_LogoImage_Source”中无效。因为从属角色属性不是关键属性,所以从属角色多重性的上限必须为'*'。

,其他导航属性相同 如果我将它们添加到上下文类中的行:

modelBuilder.Entity<Organization>().HasOptional(o => o.EconomyInfo).WithOptionalPrincipal();
modelBuilder.Entity<Image>().HasOptional(i => i.Economyinfo).WithOptionalPrincipal();

迁移将毫无错误地生成,但是会很奇怪:

public override void Up()
    {
        CreateTable(
            "dbo.EconomyInfoes",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),

                    LogoImage_Id = c.Int(nullable: false),
                    Organization_Id = c.Int(nullable: false),
                    Image_Id = c.Int(),
                    Organization_Id1 = c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Images", t => t.LogoImage_Id, cascadeDelete: true)
            .ForeignKey("dbo.Organizations", t => t.Organization_Id)
            .ForeignKey("dbo.Images", t => t.Image_Id)
            .ForeignKey("dbo.Organizations", t => t.Organization_Id1)
            .Index(t => t.LogoImage_Id)
            .Index(t => t.Organization_Id)
            .Index(t => t.Image_Id)
            .Index(t => t.Organization_Id1);

    }

我知道我可以编辑所生成的EF迁移代码以使其指定我的需求,但是没有必要浪费很多时间来处理EF,然后放弃并解决它,而且这样做并不实用。我的同事以后再使用它,请先感谢

1 个答案:

答案 0 :(得分:0)

我刚刚放弃,决定继续工作,为我的新实体编写自己的迁移,并告诉上下文在即将生成的迁移中忽略它。

所以我的解决方法是这样:

在上下文类中:

// Regarding unPossible configuration to make pre defined property as foreign key in one to zero or one relationship
// we wrote our migration and tell the context to ignore our model and its navigation property from other model classes
modelBuilder.Entity<Organization>().Ignore(o => o.EconomyInfo);
modelBuilder.Entity<Image>().Ignore(i => i.Economyinfo);
modelBuilder.Ignore<EconomyInfo>();

组织班级:

public virtual EconomyInfo EconomyInfo { get; set; }

图片类别:

public virtual EconomyInfo Economyinfo { get; set; }

人工书面迁移:

    using System;
    using System.Data.Entity.Migrations;

    public partial class EconomyInfo : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.EconomyInfoes",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),

                    LogoImage_Id = c.Int(nullable: false),
                    Organization_Id = c.Int(nullable: false),

                })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Images", t => t.LogoImage_Id)
                .ForeignKey("dbo.Organizations", t => t.Organization_Id)
                .Index(t => t.LogoImage_Id)
                .Index(t => t.Organization_Id);

        }

        public override void Down()
        {
            DropForeignKey("dbo.EconomyInfoes", "Organization_Id", "dbo.Organizations");
            DropForeignKey("dbo.EconomyInfoes", "LogoImage_Id", "dbo.Images");
            DropIndex("dbo.EconomyInfoes", new[] { "Organization_Id" });
            DropIndex("dbo.EconomyInfoes", new[] { "LogoImage_Id" });
            DropTable("dbo.EconomyInfoes");
        }
    }

您也可以参考此问题以获取更多信息How do I specify the foreign key in a one-to-one/zero relationship?

注意:我将书面迁移应用于数据库工作正常,尝试生成新迁移,也按预期工作,但尚未通过代码测试与我的新模型类一起使用。希望以后不会有任何混乱,