简体   繁体   English

AspNetUsers的ID作为单独表中的外键,一对一的关系

[英]AspNetUsers' ID as Foreign key in separate table, one-to-one relationship

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. 我上下打量,尝试了所有不同的方法,能够将AspNetUser表的外键存储在一个单独的Customer表中。 I'm still new at ASP.NET and the Entity Framework, but I've read quite a few posts and documentations. 我还是ASP.NET和实体框架的新手,但我已经阅读了不少帖子和文档。

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'. 无法确定类型“TestApplication.Models.Customer”和“TestApplication.Models.ApplicationUser”之间关联的主要结尾。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 必须使用关系流畅API或数据注释显式配置此关联的主要结尾。

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 我也试过这个人的方法: 必须使用关系流畅的API或数据注释显式配置此关联的主要结尾

So I commented out the ForeignKey annotations and used the person's suggestion, using the "modelBuilder" approach. 所以我注释掉了ForeignKey注释并使用了人的建议,使用了“modelBuilder”方法。 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. 当我更新我的数据库时,AspNetUsers表中的'Id'在Customers表中(这很好),但CustomerID作为ForeignKey也在AspNetUsers表中,这不是我想要的。

What I want, is the AspNetUsers' 'Id' to be in the Customers table as a ForeignKey. 我想要的是AspNetUsers的'Id'作为ForeignKey在Customers表中。

In a one-to-one relation the "child" table, in your case Customer , should have the same primary key as the related table, ie the foreign key. 在一对一关系中,“子”表(在您的情况下为Customer )应具有与相关表相同的主键,即外键。

The code sample you have supplied means that, in Customer you will have a PK named CustomerID which is different from UserId . 您提供的代码示例意味着,在Customer您将拥有一个名为CustomerID的PK,它与UserId不同。

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: ForeignKeyAttribute的MSDN声明:

If you add the ForeigKey attribute to a foreign key property, you should specify the name of the associated navigation property. 如果将ForeigKey属性添加到外键属性,则应指定关联导航属性的名称。 If you add the ForeigKey attribute to a navigation property, you should specify the name of the associated foreign key(s). 如果将ForeigKey属性添加到导航属性,则应指定关联外键的名称。

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. 我将此解释为应该可以将ForeignKey属性添加到导航属性或外键属性,并且这两种方式都应该有效,但显然不行。 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; }
}

I know this post is 2 years old, but a better solution would be to use Fluent API to set the foreign key (rather than using [ForeignKey] attribute in your Customer class. Here is how you would do it: 我知道这篇文章已有2年历史,但更好的解决方案是使用Fluent API设置外键(而不是在Customer类中使用[ForeignKey]属性。以下是如何做到这一点:

public class Customer
{
    public int CustomerID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Customer Customer { get; set; }
}

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

    public DbSet<Customer> Customers { get; set; }

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

        // one-to-zero or one relationship between ApplicationUser and Customer
        // UserId column in Customers table will be foreign key
        modelBuilder.Entity<ApplicationUser>()
            .HasOptional(m => m.Customer)
            .WithRequired(m => m.ApplicationUser)
            .Map(p => p.MapKey("UserId"));
    }

}

This would create a UserId column in your Customers table that is a foreign key to AspNetUsers table. 这将在Customers表中创建一个UserId列,该列是AspNetUsers表的外键。 You can omit .Map(p => p.MayKey("UserId")) and EF will name the foreign key ApplicationUser_Id by convention. 您可以省略.Map(p => p.MayKey("UserId")) ,EF将按惯例命名外键ApplicationUser_Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM