我Identity在我的 ASP.NET Core MVC 專案中使用。除此之外,我還有 2 個模型需要加入表中Id。AspNetUsers所以,我使用了 3 次遷移,1 次是Add-Migration AddAuthentication,另外 2 次用于模型。然后,通過使用Scaffold-DbContext我從資料庫中制作模型。這是問題所在,在那之后,我收到了這個錯誤:
The entity type 'AspNetUserLogin' requires a primary key to be defined.
登錄后,我曾經在我的專案中遇到該錯誤,但是,正如這里所說,我剛剛洗掉了那些 getter 和 setter。沒用。這是我最后一次嘗試,我嘗試手動創建該列,但現在,我在Update-Database.
這是自動創建的遷移的更改部分 - 沒有Id列:
migrationBuilder.CreateTable(
name: "AspNetUserLogins",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
LoginProvider = table.Column<string>(type: "nvarchar(450)", nullable: false),
ProviderKey = table.Column<string>(type: "nvarchar(450)", nullable: false),
ProviderDisplayName = table.Column<string>(type: "nvarchar(max)", nullable: true),
UserId = table.Column<string>(type: "nvarchar(450)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUserLogins", x => x.Id);
table.ForeignKey(
name: "FK_AspNetUserLogins_AspNetUsers_UserId",
column: x => x.UserId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
我完全不知道出了什么問題。提前感謝您的幫助!
uj5u.com熱心網友回復:
要創建關系并向 AspNetUsers 物體添加新列,您必須定義必須從 IdentityUser 繼承的額外類。這是示例
public class AspNetUserLogins
{
[Key]
public Guid Id {get; set;}
public Guid UserId {get; set;}
public virtual ApplicationUser User { get; set; }
}
public class ApplicationUser : IdentityUser<Guid>
{
public ApplicationUser()
{
aspNetUserLogins = new List<AspNetUserLogins>();
}
public bool IsActive { get; set; }
public bool IsDeleted { get; set; } = false;
public virtual ICollection<AspNetUserLogins> aspNetUserLogins { get; set; }
}
在背景關系類上,您必須像這樣設定。
public class YourDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public YourDbContext(DbContextOptions<YourDbContext> options)
: base(options)
{
}
}
這是我認為修改身份表的好方法。
uj5u.com熱心網友回復:
其實,這里的答案是非常正確的。在遷移和創建表之后,我已經完成Scaffold-DbContext并在其他臨時檔案夾中獲得了模型。然后,我只是將它復制到Models檔案夾中,但是,我稍微改變了背景關系。我洗掉了所有 ASP 員工,只保留了我的桌子。所以現在,它看起來像這樣:
using System;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
#nullable disable
namespace Project.Models
{
public partial class WebBankDbContext : IdentityDbContext<IdentityUser>
{
public WebBankDbContext()
{
}
public WebBankDbContext(DbContextOptions<WebBankDbContext> options)
: base(options)
{
}
public virtual DbSet<Account> Accounts { get; set; }
public virtual DbSet<Transaction> Transactions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=(LocalDB)\\MSSQLLocalDB;Database=WebBank;Trusted_Connection=True");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
重要在自動創建的身份驗證遷移中,我必須為幾個表添加 id,以避免該the entity type 'AspNetUserLogin' requires a primary key to be defined錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493140.html
標籤:C# 网 。网 asp.net-mvc asp.net 核心
