【发布时间】:2022-08-23 14:28:07
【问题描述】:
我在 AppUser 类中使用新列扩展了 IdentityUser。
public class AppUser: IdentityUser
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
在另一个模型中,我为 AppUser 创建了一个外键和导航属性,如下所示:
public abstract class TestEntity
{
[Key]
public int Id { get; set; }
public bool IsActive { get; set; }
public string UserId { get; set; }
public virtual AppUser User { get; set;}
}
然后为了保存,我使用以下代码:
testEntity.UserId = this.userManager.GetUserId(Request.HttpContext.User);
await this.context.SaveChangesAsync();
我得到的错误说:
Detail: Key (UserId)=(636511f2-a17b-47ef-9849-acb26a2ddd96) is not present in table \"AppUser\".
但是,此键在 AspNetUsers 表中作为 ID 存在。或许值得一提的是,TestEntity 与 AppUser 位于不同的数据库上下文中。
-
TestEntity 是一个无法实例化的抽象类。你是如何添加迁移的?
-
抱歉,我在使用 repo 模式时不得不最小化代码,忘记删除抽象关键字..所以抽象关键字不存在..
标签: asp.net-mvc asp.net-core entity-framework-core