Entity Framework Core:一对一关系

发布于 2025-01-10 16:44:01 字数 3025 浏览 0 评论 0 原文

我正在尝试建立一对一的关系,但没有成功。

数据模型如下图所示。一名用户拥有一个角色:

这是我使用的代码。首先是 UserRole 和 User 实体的模型,然后是映射。

模型 UserRole:

public class UserRole 
{
    public long Id { get; set; }
    public bool? IsActive { get; set; }
    public DateTime? AddedWhen { get; set; }
    public DateTime? UpdatedWhen { get; set; }
    public string Name { get; set; }
    public ICollection<User> Users { get; set; }
}

模型用户:

public class User 
{
    public long Id { get; set; }
    public bool? IsActive { get; set; }
    public DateTime? AddedWhen { get; set; }
    public DateTime? UpdatedWhen { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }

    public UserRole UserRole { get; set; }
    public long UserRoleId { get; set; }
}

UserRole 的映射:

public class UserRoleMap
{
    public UserRoleMap(EntityTypeBuilder<UserRole> entityBuilder)
    {
        entityBuilder.Property(t => t.Name).IsRequired();
        entityBuilder.HasKey(t => t.Id);
        entityBuilder.Property(t => t.IsActive).HasDefaultValueSql("1");
        entityBuilder.Property(t => t.UpdatedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
        entityBuilder.Property(t => t.AddedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
    }
}

User 的映射:

  public class UserMap
  {   
        public UserMap(EntityTypeBuilder<User> entityBuilder)
        {

            entityBuilder.HasKey(t => t.Id);
            entityBuilder.Property(t => t.IsActive).HasDefaultValueSql("1");
            entityBuilder.Property(t => t.UpdatedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
            entityBuilder.Property(t => t.AddedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
            entityBuilder.Property(t => t.Email).IsRequired();
            entityBuilder.Property(t => t.Password).IsRequired();
            entityBuilder.Property(t => t.UserName).IsRequired();

            entityBuilder.
                HasOne<UserRole>(s => s.UserRole)
                .WithMany(g => g.Users)
                .HasForeignKey(s => s.UserRoleId);
        }
    }

“常规”映射类:

 public class ApplicationContext : DbContext
 {
        public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            _ = new UserMap(modelBuilder.Entity<User>());
            _ = new UserRoleMap(modelBuilder.Entity<UserRole>());
        }
    }

我一直在尝试找到的其他解决方案,但没有一个有效。 当我获得一个用户时,我希望看到它的角色。但属性 UserRole 始终为空。

I'm trying to do a one-to-one relationship without success.

The data model is in the image below. One user has one role:

enter image description here

Here is the code that I've used. First the models for UserRole and User entities and then the mappings.

Model UserRole:

public class UserRole 
{
    public long Id { get; set; }
    public bool? IsActive { get; set; }
    public DateTime? AddedWhen { get; set; }
    public DateTime? UpdatedWhen { get; set; }
    public string Name { get; set; }
    public ICollection<User> Users { get; set; }
}

Model User:

public class User 
{
    public long Id { get; set; }
    public bool? IsActive { get; set; }
    public DateTime? AddedWhen { get; set; }
    public DateTime? UpdatedWhen { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }

    public UserRole UserRole { get; set; }
    public long UserRoleId { get; set; }
}

The mappings for UserRole:

public class UserRoleMap
{
    public UserRoleMap(EntityTypeBuilder<UserRole> entityBuilder)
    {
        entityBuilder.Property(t => t.Name).IsRequired();
        entityBuilder.HasKey(t => t.Id);
        entityBuilder.Property(t => t.IsActive).HasDefaultValueSql("1");
        entityBuilder.Property(t => t.UpdatedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
        entityBuilder.Property(t => t.AddedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
    }
}

The mappings for User:

  public class UserMap
  {   
        public UserMap(EntityTypeBuilder<User> entityBuilder)
        {

            entityBuilder.HasKey(t => t.Id);
            entityBuilder.Property(t => t.IsActive).HasDefaultValueSql("1");
            entityBuilder.Property(t => t.UpdatedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
            entityBuilder.Property(t => t.AddedWhen).HasDefaultValueSql("CURRENT_TIMESTAMP");
            entityBuilder.Property(t => t.Email).IsRequired();
            entityBuilder.Property(t => t.Password).IsRequired();
            entityBuilder.Property(t => t.UserName).IsRequired();

            entityBuilder.
                HasOne<UserRole>(s => s.UserRole)
                .WithMany(g => g.Users)
                .HasForeignKey(s => s.UserRoleId);
        }
    }

The "general" mappings class:

 public class ApplicationContext : DbContext
 {
        public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            _ = new UserMap(modelBuilder.Entity<User>());
            _ = new UserRoleMap(modelBuilder.Entity<UserRole>());
        }
    }

I've been trying other solutions that I found, but none of them worked.
When I get a user I wish to see its role. But the property UserRole is always null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文