c# - 将 IdentityUser 关联到 EF Core 中的实体

标签 c# asp.net-core entity-framework-core

下面是 IdentityUser 子类,和一个简单的实体。如何在实体类中引用 IdentityUser?此应用程序正在使用 Entity Framework Core。

ApplicationUser.cs

using Microsoft.AspNetCore.Identity;

namespace MyProject.Models
{
    public class ApplicationUser : IdentityUser
    {

    }
}

Entity.cs

namespace MyProject.Models
{
    public class Entity
    {
        public int EntityId { get; set; }

        /* What belongs here to associate to a user? */
    }
}

最佳答案

约定优于配置的方式是:

public class Entity
{
    public int EntityId { get; set; }
    public string ApplicationUserId { get; set; }

    public ApplicationUser ApplicationUser { get; set; }
}

以下是可选的,如果不需要获取用户的所有实体,可以省略。

public class ApplicationUser : IdentityUser
{
    public ICollection<Entity> Entities { get; set; }
}

关于c# - 将 IdentityUser 关联到 EF Core 中的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51115799/

相关文章:

c# - ASP.NET 核心在单例服务上调用异步初始化

c# - linq 到 XML 字符串

c# - 如何减慢旋转速度?

c# - 如何通过 POST 将参数传递给 Azure 函数?

c# - Asp.net Core 中的 HttpContext 异步安全吗?

asp.net - INSERT语句与FOREIGN KEY约束冲突

c# - 矩形,它在哪里?

c# - 如何在 .net 核心中添加 ws-security header ?

c# - Entity Framework Core 两个对象作为主键

c#-4.0 - Entity Framework 核心 2.1 : The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'