【问题标题】:Extra Foreign Key in Code First MigrationsCode First 迁移中的额外外键
【发布时间】:2015-12-17 17:28:06
【问题描述】:

我对实体框架和代码优先迁移有点陌生,所以我希望这是一个容易回答的问题。我正在尝试在 ApplicationUser(来自 ASP.NET 身份)和成员之间创建一对一的关系。我有一个会员类:

public class Member
{
    public int ID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual Address Address { get; set; }
    public UserStatus Status { get; set; }
    public DateTime CreateDate { get; set; }
    public virtual string ApplicationUserID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; } 
}

还有一个 ApplicationUserClass:

public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {

        }       

        public virtual Member Member { get; set; }

    }

在我的 DBContext(继承 IdentityDbContext)中,我有以下配置:

    modelBuilder.Entity<ApplicationUser>()
            .HasOptional(t => t.Member).WithOptionalPrincipal();

    base.OnModelCreating(modelBuilder);

当我运行代码第一次迁移时,我得到了这个:

 CreateTable(
            "dbo.Members",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    FirstName = c.String(),
                    LastName = c.String(),
                    Status = c.Int(nullable: false),
                    CreateDate = c.DateTime(nullable: false),
                    ApplicationUserID = c.String(maxLength: 128),
                    Address_ID = c.Int(),
                    ApplicationUser_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Addresses", t => t.Address_ID)
            .ForeignKey("dbo.AspNetUsers", t => t.ApplicationUser_Id)
            .ForeignKey("dbo.AspNetUsers", t => t.ApplicationUserID)
            .Index(t => t.ApplicationUserID)
            .Index(t => t.Address_ID)
            .Index(t => t.ApplicationUser_Id);

请注意,我有 2 个外键,ApplicationUserIDApplicationUser_Id。我想尝试使用 FluentAPI 做所有事情(即不是数据注释)。我将如何配置它以使 EF 使用 ApplicationUserID,即我在课堂上拥有的字符串 ID?我以为 Class+ID 是约定俗成的,那为什么还要创建另一个外键呢?

【问题讨论】:

  • 您可以将您的 Member 类的 modelBuilder 配置代码添加到您的问题中吗?

标签: c# .net entity-framework ef-code-first


【解决方案1】:

我相信你应该这样更新你的配置:

modelBuilder.Entity<Member>()
            .HasOptional(x => x.ApplicationUser)
            .WithMany()
            .HasForeignKey(x => x.ApplicationUserID);

这是 EntityFramework 处理一对一关系的方式,您必须以这种方式映射它并在您的数据库表上引入 UNIQUE 约束。

关于这个案例的更多信息在这里:http://weblogs.asp.net/manavi/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations

这是链接中的引用:

原因很简单:Code First(以及一般的 EF)本身并没有 支持一对一的外键关联。事实上,EF 并没有 支持任何涉及独特约束的关联场景 全部。幸运的是,在这种情况下,我们并不关心目标端有什么 的关联,所以我们可以把它当作一对一的关联 没有很多部分。我们想要的只是表达“这个实体(用户) 有一个属性是对另一个实体的实例的引用 (地址)”并使用外键字段来表示该关系。 基本上EF还是认为关系是多对一的。这 是针对当前 EF 限制的一种解决方法,它带有两个 后果:首先,EF 不会为我们创建任何额外的约束 要将这种关系强制为一对一,我们需要手动 自己创造。第二个限制是这种缺乏支持 强加给我们更重要:一对一的外键关联 不能是双向的(即我们不能在 地址类)。

【讨论】:

  • 奇怪的是我把它改成了 ApplicationUser_Id 并创建了 ApplicationUser_Id 和 ApplicationUser_Id1.....
猜你喜欢
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多