外键Microsoft.AspNet.Identity.EntityFramework.IdentityUser? [英] Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser?
问题描述
我在VS 2013年和刚刚创建MVC应用程序。
I'm in VS 2013 and have just created an MVC application.
我创建一个对象,我打算在一个外键AspNetUsers表生成的数据库。该项目确实有,看起来像一个属性栏比赛的AspNetUsers表中ApplicationUser(从IdentityUser派生)。
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的Code First迁移?
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语句外键,但如果我想从代码首先再次更新,我可能会错误(数据库不匹配目前老迁移或类似的东西)。
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).
我们如何正确外商主要参考这些成员表?
How do we properly foreign-key reference those membership tables?
我也试图与匹配AspNetUsers表的属性创建一个AspNetUser类。而不是在客户端类的公共ApplicationUser,我宣布大众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
类,添加一个 ForeignKey的
属性,
I would do the following: In the ApplicationUser
class, add a ForeignKey
attribute,
public ApplicationUser : IdentityUser {
[ForeignKey("UserID")]
public virtual ICollection<MyCustomUser> MyCustomUsers{ get; set; }
}
和在模型中要跟踪其所属的用户,
and in your model where you want to track to which user it belongs,
public MyObject {
public string UserId { get; set; }
// other properties
}
您别吨需要存储整个 ApplicationUser
实例中的为MyObject
类和用户名
将自动生成。重要的是,是的类型为字符串
,由于是 ID
的 ApplicationUser
!
You don't need to store the whole ApplicationUser
instance in the MyObject
class, and the UserID
will be generated automatically. It is important that is is of type string
, as is the ID
of the ApplicationUser
!
这篇关于外键Microsoft.AspNet.Identity.EntityFramework.IdentityUser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!