我使用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 中有什么问题?我有什么不理解的地方,需要调整的地方是什么?