Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser?

c# ef-code-first ef-migrations entity-framework poco

Question

I'm using Visual Studio 2013 and I just made an MVC application.

I'm building an object that will serve as a foreign key to the database's AspNetUsers table. There is an ApplicationUser in the project that appears to be a property-column match with the AspNetUsers database (it derives from IdentityUser).

How do we declare a foreign key to this in the right way?

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

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

   // other properties
}

In order to have a collection of MyObjects, I now modify ApplicationUser:

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

This appears to be the one-to-many EF Code First method. However, I keep getting issues that claim Identity members (IdentityUserLogin, IdentityUserRole, etc.) do not have defined keys when I update the database. It's possible that such classes weren't intended to take part in EF Code First Migrations.

If I wanted to update again from Code First, I could go "to the back" and add the foreign key using SQL queries, but I might get issues (that the database doesn't now match the earlier migration, for example).

How should those membership tables be appropriately foreign-key referenced?

I also made an effort to design an AspNetUser class that had properties that matched those in the AspNetUsers database. I declared "public AspNetUser" on the Client class rather than "public ApplicationUser". An automatic migration was unsuccessful as a result of this since it would have caused data loss.

So what do you do?

1
8
10/29/2014 10:19:51 PM

Popular Answer

One-to-many relationships are simple to establish betweenApplicationUser and MyObject Then add a foreign key named "UserId" to yourMyObjects table. I appreciate this method because it adheres to EF rules and eliminates the requirement for[ForeignKey] feature of 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"));
    }
}

Observe how your application uses the Fluent API to generate a "UserId" foreign key.MyObjects table. Without the Fluent API, this approach would still function, but your foreign key column in your database would be called "ApplicationUser Id."MyObjects by convention, a table.

3
10/12/2016 11:06:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow