我使用IEntityTypeConfigurations和FluentMigrator迁移来设置我的数据访问.大体上,我认为我对配置实体和表以解决关系有了基本的了解,在某种程度上它确实起作用了.

However now my UserAccounts UserRole is always null when querying my desired account from the database. Same happens with the permissions for my roles.
Before the current snippet I have had the accounts that utilize a role as a list and I established a relationship between those, but that did not work either. I am a little bit confused and at this point I am just trying stuff out without any understanding, to be honest.

据我所知,存储实体工作得很好,就像我想要的那样.在将上下文中的更改保存到数据库后,实体处于我希望它们所处的状态.这意味着所有权限都存在于我的角色中,该角色存在于我的帐户中,具有我想要的所有数据.

public class UserAccount
{
    public int Id { get; set; }
    public string Email { get; set; } = string.Empty;
    public string PhoneNumber { get; set; } = string.Empty;
    public string LoginName { get; set; } = string.Empty;
    public string PasswordHash { get; set; } = string.Empty;
    public DateTime PasswordChanged { get; set; } = DateTime.Now;
    public int RoleId? { get; set; }
    public virtual UserRole? Role { get; set; }
}

public class UserAccountEntityMap : IEntityTypeConfiguration<UserAccount>
{
    public void Configure(EntityTypeBuilder<UserAccount> builder)
    {
        builder.ToTable("UserAccounts");
        builder.HasKey(x => x.Id);
        
        builder.Property(u => u.Id).HasColumnName("Id").UseIdentityColumn();
        builder.Property(u => u.Email).HasColumnName("Email");
        builder.Property(u => u.PhoneNumber).HasColumnName("PhoneNumber");
        builder.Property(u => u.LoginName).HasColumnName("LoginName");
        builder.Property(u => u.PasswordHash).HasColumnName("PasswordHash");
        builder.Property(u => u.PasswordChanged).HasColumnName("PasswordChanged");
        
        builder.HasOne(u => u.Role)
            .WithMany()
            .HasForeignKey(u => u.RoleId);
    }
}

// Migration creating the table
public override void Up()
{
    if (!Schema.Table("UserAccounts").Exists())
    {
        Create.Table("UserAccounts")
            .WithColumn("Id")
                .AsInt32()
                .NotNullable()
                .PrimaryKey()
                .Identity()
            .WithColumn("Email")
                .AsString()
            .WithColumn("PhoneNumber")
                .AsString()
            .WithColumn("LoginName")
                .AsString()
            .WithColumn("PasswordHash")
                .AsString()
            .WithColumn("PasswordChanged")
                .AsDateTime2()
            .WithColumn("RoleId")
                .AsInt32()
                .ForeignKey();
        
        Create.ForeignKey("FK_UserAccount_UserRole")
            .FromTable("UserAccounts")
                .ForeignColumn("RoleId")
            .ToTable("UserRoles")
                .PrimaryColumn("Id");
    }
}
public class UserRole
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public DateTime LastChanged { get; set; } = DateTime.Now;
    public virtual List<UserRolePermission> Permissions { get; set; } = new();
}

public class UserRoleEntityMap : IEntityTypeConfiguration<UserRole>
{
    public void Configure(EntityTypeBuilder<UserRole> builder)
    {
        builder.ToTable("UserRoles");
        builder.HasKey(t => t.Id);
        
        builder.Property(r => r.Id).HasColumnName("Id").UseIdentityColumn();
        builder.Property(r => r.Name).HasColumnName("Name");
        builder.Property(r => r.Description).HasColumnName("Description");
        builder.Property(r => r.LastChanged).HasColumnName("LastChanged");
        
        builder.HasMany(r => r.Permissions)
            .WithOne()
            .HasForeignKey(p => p.RoleId);
    }
}

// Migration creating the table
public override void Up()
{
    if (!Schema.Table("UserRoles").Exists())
    {
        Create.Table("UserRoles")
            .WithColumn("Id")
                .AsInt32()
                .NotNullable()
                .PrimaryKey()
                .Identity()
            .WithColumn("Name")
                .AsString()
            .WithColumn("Description")
                .AsString()
            .WithColumn("LastChanged")
                .AsDateTime2();
    }
}

我try 了很多方法,甚至参考了我们在工作中使用的配置,我正在做学徒工作--我的设置几乎是在模仿它--但我就是无法让它起作用.

我试图使我的相关属性成为虚拟的,但没有成功,我在新数据库上多次更改迁移和实体配置.许多在线资源只是参考和提供自动生成的模型配置和迁移,甚至那些没有给我带来更多帮助的资源.

So I have a few concrete questions

  • 外键几乎就是描述与数据库对象相关的表和列的"引用".多个列可以形成一个外键.我对这一点是正确的,还是我误解了我到目前为止收集的信息?

  • 主键相当于识别符,显然是唯一的,可以由多个列组成,这些列是唯一描述数据库条目所必需的.我说的对吗?

  • 可以在没有密钥的情况下在实体配置中设置实体,有哪些用例?

  • 很明显:我在上面的try 中有什么问题?我有什么不理解的地方,需要调整的地方是什么?

推荐答案

你在偷懒装货吗?尝尝这个

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLazyLoadingProxies()
        .UseSqlServer(myConnectionString);

https://learn.microsoft.com/en-us/ef/core/querying/related-data/lazy

.net相关问答推荐

条带连接支付—引发异常

.NET Blazor-使用子组件中的处理程序方法进行双向数据绑定

从窗体中移除另一个控件中引用的控件时获取设计时通知

在 .NET 中使用 AES 解密时缺少后半字节

MassTransit RespondAsync 无法返回空值

正则表达式在空格上拆分,除非在引号中

xunit Assert.ThrowsAsync() 不能正常工作?

我应该创建一个 DateRange 对象吗?

BackgroundWorker 中未处理的异常

将跟踪输出重定向到控制台

如何结合 ||条件语句中的运算符

变量MyException已声明但从未使用

什么是 C# 中的自动属性,它们的用途是什么?

哪个密码字符在 winforms 文本框中显示黑点 (•)?

如何修复 .NET Windows 应用程序在启动时崩溃并出现异常代码:0xE0434352?

C# 应用程序中的全局键盘捕获

.NET 中的 java.lang.IllegalStateException?

在 C#/.NET 中合并两个图像

从不同程序集中的类名中解析类型

枚举和匹配属性的 C# 命名约定