Microsoft.AspNet.Identity.EntityFramework.IdentityUser的外键? [英] Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser?

查看:119
本文介绍了Microsoft.AspNet.Identity.EntityFramework.IdentityUser的外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2013中刚刚创建了一个MVC应用程序。

I'm in VS 2013 and have just created an MVC application.

我正在创建一个对象,我打算在AspNetUsers表中有一个外键生成的数据库。该项目确实有一个ApplicationUser(来自IdentityUser),它与AspNetUsers表格类似于一个属性列匹配。

I'm creating an object I intend to have a foreign key to the AspNetUsers table in the resulting database. The project does have an ApplicationUser (deriving from IdentityUser) that looks like a property-column match with the AspNetUsers table.

我们如何正确地声明一个外键?

How do we properly declare a foreign key to this?

public MyObject
{
   public string UserId { get; set; }

   [ForeignKey("UserId")]
   public ApplicationUser User { get; set;}

   // other properties
}

现在,我修改ApplicationUser以收集MyObjects:

Now, I modify ApplicationUser to have a collection of MyObjects:

public ApplicationUser : IdentityUser
{
     public virtual ICollection<MyObject> MyObjects { get; set; }    
}

这似乎是如何在EF代码中进行一对多的操作第一。但是,当我更新数据库时,我收到的错误就是身份成员(IdentityUserLogin,IdentityUserRole等)没有定义密钥。也许这些课程不是要参加EF代码优先迁移?

This seems to be how to do one-to-many in EF Code First. However, when I update-database, I'm getting the errors that say Identity members (IdentityUserLogin, IdentityUserRole, etc.) have no keys defined. Perhaps those classes were not meant to participate in EF Code First Migrations?

我可以去返回,并通过SQL语句添加外键,但如果我想从Code First再次更新,我可能会收到错误(数据库目前不符合旧的迁移或类似的情况)。

I could go "to the back" and add the foreign key via SQL statements, but if I wanted to update again from Code First, I might get errors (that the database doesn't currently match the older migration or something like that).

关键参考这些会员表?

我还尝试创建一个具有AspNetUsers表匹配属性的AspNetUser类。客户端类不是public ApplicationUser,而是声明了public AspNetUser。这样做会导致迁移失败 - 自动迁移不会被应用,因为它会导致数据丢失。

I also tried to create an AspNetUser class with matching properties of the AspNetUsers table. Instead of "public ApplicationUser" on the Client class, I declared "public AspNetUser". Doing this resulted in a migration failure - "Automatic migration was not applied because it would result in data loss."

那么该怎么办?

推荐答案

很容易在 ApplicationUser 和<$ c之间创建一对多的关系$ c> MyObject 并在您的 MyObjects 表中添加一个UserId外键。我喜欢这个解决方案的是它符合EF约定,您的模型中不需要 [ForeignKey] 属性:

It is easy to create a one-to-many relationship between ApplicationUser and MyObject and add a "UserId" foreign key in your MyObjects table. What I like about this solution is that it follows EF conventions and there is no need for [ForeignKey] attribute in your model:

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<MyObject> MyObjects { get; set; }
}

public class MyObject
{
    public int MyObjectId { get; set; }

    public string MyObjectName { get; set; }

    // other properties

    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public DbSet<MyObject> MyObjects { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyObject>()
            .HasRequired(c => c.ApplicationUser)
            .WithMany(t => t.MyObjects)
            .Map(m => m.MapKey("UserId"));
    }
}

请注意使用Fluent API创建UserId 外键在你的 MyObjects 表中。该解决方案仍然可以在不添加Fluent API的情况下工作,但是您的外键列将按照惯例在 MyObjects 表中命名为ApplicationUser_Id。

Notice the use of Fluent API to create a "UserId" foreign key in your MyObjects table. This solution would still work without adding the Fluent API, but then your foreign key column would be named "ApplicationUser_Id" in your MyObjects table by convention.

这篇关于Microsoft.AspNet.Identity.EntityFramework.IdentityUser的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆