AspNetUsers的ID作为外键在单独的表中,一对一的关系 [英] AspNetUsers' ID as Foreign key in separate table, one-to-one relationship
问题描述
我已经向上和向下查看,尝试了所有不同的和各种方法能够在单独的Customer表中存储AspNetUser表的外键。我还是新在ASP.NET和Entity框架,但我已经阅读了很多帖子和文档。
目前这是我有
MODELS
public class Customer
{
[ Display(Name =Customer ID)]
public int CustomerID {get;组; }
public string UserId {get;组; }
[ForeignKey(UserId)]
public virtual ApplicationUser ApplicationUser {get;组; }
}
public class ApplicationUser:IdentityUser
{
public virtual Customer Customer {get;组; }
}
public class ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
public DbSet< Customer>客户{get;组; }
public ApplicationDbContext()
:base(DefaultConnection)
{
}
}
我得到这个错误,报价
无法确定类型TestApplication.Models.Customer和TestApplication.Models.ApplicationUser之间的关联的主要端。此关联的主要端点必须使用关系流利API或数据注释显式配置。
我也尝试过此人的方法这里:此关联的主要目的必须使用关系流利API或数据注释显式配置
所以我注释掉了ForeignKey注释,并使用了人的建议,使用modelBuilder方法。当我更新我的数据库时,来自AspNetUsers表的'Id'在Customers表中(这是好的),但CustomerID作为ForeignKey也在AspNetUsers表中,这不是我想要的。
我想要的是AspNetUsers的Id作为ForeignKey在Customers表中。
解决方案在一个一对一关系中,child表,在您的情况下
Customer
,应该具有与相关表相同的主键,
您提供的代码示例意味着在
Customer
中,您将有一个PK命名CustomerID
与UserId
不同。
在您的情况下工作(未测试):
public class Customer
{
[Key]
public string UserId {get;组; }
[ForeignKey(UserId)]
public virtual ApplicationUser ApplicationUser {get;组; }
}
public class ApplicationUser:IdentityUser
{
public virtual Customer Customer {get;组; }
}
编辑:
MSDN for ForeignKeyAttribute 状态:
如果将ForeigKey属性添加到外键属性,应该指定相关导航属性的名称。如果你
将ForeigKey属性添加到导航属性,你应该
指定关联的外键的名称。
我解释这是应该可以添加ForeignKey属性到导航属性或外键属性,并且任何一种方式应该工作,但显然不是。
public class Customer
{
[Key,ForeignKey (ApplicationUser)]
public string UserId {get;组; }
public virtual ApplicationUser ApplicationUser {get;组; }
}
public class ApplicationUser:IdentityUser
{
public virtual Customer Customer {get;组; }
}
I have looked up and down, tried all the different and various ways of being able to store a foreign key of the AspNetUser table in a separate Customer table. I'm still new at ASP.NET and the Entity Framework, but I've read quite a few posts and documentations.
Currently this is what I have
MODELS
public class Customer { [Display (Name="Customer ID")] public int CustomerID { get; set; } public string UserId { get; set; } [ForeignKey("UserId")] public virtual ApplicationUser ApplicationUser { get; set; } } public class ApplicationUser : IdentityUser { public virtual Customer Customer { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public DbSet<Customer> Customers { get; set; } public ApplicationDbContext() : base("DefaultConnection") { } }
I get this error, quote
Unable to determine the principal end of an association between the types 'TestApplication.Models.Customer' and 'TestApplication.Models.ApplicationUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I also tried this person's method found here: The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations
So I commented out the ForeignKey annotations and used the person's suggestion, using the "modelBuilder" approach. And when I updated my database, the 'Id' from the AspNetUsers table was in the Customers table (which is good), but the CustomerID as a ForeignKey was also in the AspNetUsers table, which is not what I want.
What I want, is the AspNetUsers' 'Id' to be in the Customers table as a ForeignKey.
解决方案In a one-to-one relation the "child" table, in your case
Customer
, should have the same primary key as the related table, i.e. the foreign key.The code sample you have supplied means that, in
Customer
you will have a PK namedCustomerID
which is different fromUserId
.This should work in your case (untested):
public class Customer { [Key] public string UserId { get; set; } [ForeignKey("UserId")] public virtual ApplicationUser ApplicationUser { get; set; } } public class ApplicationUser : IdentityUser { public virtual Customer Customer { get; set; } }
Edit:
MSDN for ForeignKeyAttribute states:
If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s).
I interpret this as that it should be possible to add the ForeignKey-attribute to either the navigation property or the foreign key property, and that either way should work, but apparently not. Moving it as per below should do the trick.
public class Customer { [Key, ForeignKey("ApplicationUser")] public string UserId { get; set; } public virtual ApplicationUser ApplicationUser { get; set; } } public class ApplicationUser : IdentityUser { public virtual Customer Customer { get; set; } }
这篇关于AspNetUsers的ID作为外键在单独的表中,一对一的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!