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

C# AspNetUsers';ID作为单独表中的外键,一对一关系,c#,asp.net,database,entity-framework,one-to-one,C#,Asp.net,Database,Entity Framework,One To One,我上下查看,尝试了所有不同的方法,可以将AspNetUser表的外键存储在单独的Customer表中。我对ASP.NET和Entity Framework还是个新手,但我读过不少文章和文档 这就是我现在拥有的 模型 public class Customer { [Display (Name="Customer ID")] public int CustomerID { get; set; } public string UserId { get; set; }

我上下查看,尝试了所有不同的方法,可以将AspNetUser表的外键存储在单独的Customer表中。我对ASP.NET和Entity Framework还是个新手,但我读过不少文章和文档

这就是我现在拥有的

模型

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")
    {
    }

}
公共类客户
{
[显示(Name=“客户ID”)]
public int CustomerID{get;set;}
公共字符串用户标识{get;set;}
[外键(“用户ID”)]
公共虚拟应用程序用户应用程序用户{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟客户客户{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext

因此,我注释掉了ForeignKey注释,并使用了此人的建议,使用“modelBuilder”方法。当我更新数据库时,AspNetUsers表中的“Id”在Customers表中(这很好),但是CustomerID作为外键也在AspNetUsers表中,这不是我想要的


我想要的是,AspNetUsers“Id”作为外键出现在Customers表中。

在一对一关系中,“子”表(在您的例子中是
Customer
)应该与相关表具有相同的主键,即外键

您提供的代码示例意味着,在
Customer
中,您将有一个名为
CustomerID
的PK,它不同于
UserId

这应该适用于您的情况(未经测试):

编辑:

国家:

如果将ForeigKey属性添加到外键属性,则 应指定关联导航属性的名称。如果你 将ForeigKey属性添加到导航属性,您应该 指定关联外键的名称

我的解释是,应该可以将ForeignKey属性添加到导航属性或外键属性中,并且这两种方法都可以,但显然不行。按照下面的步骤移动它就可以了

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; }
}

我知道这篇文章已经有2年了,但更好的解决方案是使用Fluent API设置外键(而不是在客户类中使用
[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"));
    }

}
公共类客户
{
public int CustomerID{get;set;}
公共虚拟应用程序用户应用程序用户{get;set;}
}
公共类应用程序用户:IdentityUser
{
公共虚拟客户客户{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():基本(“DefaultConnection”)
{
}
公共数据库集客户{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
//ApplicationUser和Customer之间的一对零或一关系
//Customers表中的UserId列将是外键
modelBuilder.Entity()
.has可选(m=>m.Customer)
.WithRequired(m=>m.ApplicationUser)
.Map(p=>p.MapKey(“UserId”);
}
}

这将在
Customers
表中创建一个
UserId
列,该列是
AspNetUsers
表的外键。您可以省略
.Map(p=>p.MayKey(“UserId”))
和EF将按惯例命名外键
应用程序用户Id

嘿,谢谢你的快速反馈。按照你说的做了,我得到了以下错误:“无法确定类型“TestApplication.Models.ApplicationUser”和“TestApplication.Models.Customer”之间关联的主体端。必须使用关系API或数据批注显式配置此关联的主体端。”。"抱歉,我想你错过了InverseProperty,因为Code first无法单独匹配这两个类中的属性。CMIIW嘿,它运行得很好,谢谢你的建议。我一直认为每个表都必须有自己的唯一ID,与其他表分开。不过,这种方法更有意义,谢谢。它非常有用灵活。很抱歉,回复太晚。根据您的评论,我无法判断问题出在哪里。如果您在SO上问了一个新问题,并发布您的
ApplicationDbContext
类的代码以及您希望向AspNetUsers主键添加外键的模型类,可能会更好。
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"));
    }

}