【问题标题】:Associate IdentityUser to an Entity in EF Core将 IdentityUser 关联到 EF Core 中的实体
【发布时间】:2018-07-01 09:23:15
【问题描述】:

下面是 IdentityUser 子类和一个简单的实体。如何在 Entity 类中引用 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? */
    }
}

【问题讨论】:

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


    【解决方案1】:

    约定优于配置的方式是:

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

    并且下面是可选的,如果你不需要为一个用户获取所有的Entities,你可以省略它。

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

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多