如何在 C# 中使用外键从其他 class 引用 class?
How to reference a class from other class with foreign key in C#?
我想在对象 UserPassword 中有一个带有对象用户的字段,它具有用户的所有属性(uuid、名称、角色...)
用户密码代码:
[Key]
public Guid Uuid { get; set; }
public Guid UserID { get; set; }
public Guid PasswordID { get; set; }
public string Value { get; set; }
public int Permissions { get; set; }
public User User { get; set; }
public Password Password { get; set; }
用户代码:
[Key]
public Guid Uuid { get; set; }
[StringLength(40, ErrorMessage = "The Name value cannot exceed 40 characters. ")]
public string Name { get; set; }
public string Rol { get; set; }
[StringLength(4096, ErrorMessage = "The PublicKey value cannot exceed 4096 characters. ")]
public string PublicKey { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RemovedDate { get; set; }
public List<UserPassword> userPassword { get; set; }
DBContext代码:
public class PasswordManagementDbContext : DbContext
{
public PasswordManagementDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Password>().HasMany(x => x.userPassword).WithOne(x => x.Password).IsRequired();
modelBuilder.Entity<User>().HasMany(x => x.userPassword).WithOne(x => x.User).IsRequired();
modelBuilder.Entity<UserPassword>().HasOne<User>().WithMany().HasForeignKey(x => x.UserID).IsRequired();
modelBuilder.Entity<UserPassword>().HasOne<Password>().WithMany().HasForeignKey(x => x.PasswordID).IsRequired();
}
public DbSet<User> Users { get; set; }
public DbSet<Password> Passwords { get; set; }
public DbSet<UserPassword> UserPasswords { get; set; }
目标是可以从 UserPassword class 中提取用户,如下所示:
string name = userPassword.User.Name;
下面是一个工作演示,您可以参考一下:
User.cs:
public class User
{
[Key]
public Guid Uuid { get; set; }
[StringLength(40, ErrorMessage = "The Name value cannot exceed 40 characters. ")]
public string Name { get; set; }
public string Rol { get; set; }
[StringLength(4096, ErrorMessage = "The PublicKey value cannot exceed 4096 characters. ")]
public string PublicKey { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RemovedDate { get; set; }
public virtual ICollection<UserPassword> Password { get; set; }// navigation property to Password
}
Password.cs:
public class Password
{
public Guid PasswordID { get; set; }
public string rol { get; set; }//other thing you want...
public virtual ICollection<UserPassword> User { get; set; } //navigation property to User
}
DbContext.cs:
public class GuidUserContext : DbContext
{
public GuidUserContext (DbContextOptions<GuidUserContext> options)
: base(options)
{
}
public DbSet<GuidUser.Models.UserPassword> UserPassword { get; set; }
public DbSet<GuidUser.Models.User> User { get; set; }
public DbSet<GuidUser.Models.Password> Password { get; set; }
}
Controller.cs:
public class HomeController : Controller
{
private readonly GuidUserContext _context;
public HomeController(GuidUserContext context)
{
_context = context;
}
public IActionResult Index()
{
string name = _context.UserPassword.Select(u=>u.User.Name).First();
return View();
}
}
结果:
我想在对象 UserPassword 中有一个带有对象用户的字段,它具有用户的所有属性(uuid、名称、角色...)
用户密码代码:
[Key]
public Guid Uuid { get; set; }
public Guid UserID { get; set; }
public Guid PasswordID { get; set; }
public string Value { get; set; }
public int Permissions { get; set; }
public User User { get; set; }
public Password Password { get; set; }
用户代码:
[Key]
public Guid Uuid { get; set; }
[StringLength(40, ErrorMessage = "The Name value cannot exceed 40 characters. ")]
public string Name { get; set; }
public string Rol { get; set; }
[StringLength(4096, ErrorMessage = "The PublicKey value cannot exceed 4096 characters. ")]
public string PublicKey { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RemovedDate { get; set; }
public List<UserPassword> userPassword { get; set; }
DBContext代码:
public class PasswordManagementDbContext : DbContext
{
public PasswordManagementDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Password>().HasMany(x => x.userPassword).WithOne(x => x.Password).IsRequired();
modelBuilder.Entity<User>().HasMany(x => x.userPassword).WithOne(x => x.User).IsRequired();
modelBuilder.Entity<UserPassword>().HasOne<User>().WithMany().HasForeignKey(x => x.UserID).IsRequired();
modelBuilder.Entity<UserPassword>().HasOne<Password>().WithMany().HasForeignKey(x => x.PasswordID).IsRequired();
}
public DbSet<User> Users { get; set; }
public DbSet<Password> Passwords { get; set; }
public DbSet<UserPassword> UserPasswords { get; set; }
目标是可以从 UserPassword class 中提取用户,如下所示:
string name = userPassword.User.Name;
下面是一个工作演示,您可以参考一下:
User.cs:
public class User
{
[Key]
public Guid Uuid { get; set; }
[StringLength(40, ErrorMessage = "The Name value cannot exceed 40 characters. ")]
public string Name { get; set; }
public string Rol { get; set; }
[StringLength(4096, ErrorMessage = "The PublicKey value cannot exceed 4096 characters. ")]
public string PublicKey { get; set; }
public DateTime CreationDate { get; set; }
public DateTime RemovedDate { get; set; }
public virtual ICollection<UserPassword> Password { get; set; }// navigation property to Password
}
Password.cs:
public class Password
{
public Guid PasswordID { get; set; }
public string rol { get; set; }//other thing you want...
public virtual ICollection<UserPassword> User { get; set; } //navigation property to User
}
DbContext.cs:
public class GuidUserContext : DbContext
{
public GuidUserContext (DbContextOptions<GuidUserContext> options)
: base(options)
{
}
public DbSet<GuidUser.Models.UserPassword> UserPassword { get; set; }
public DbSet<GuidUser.Models.User> User { get; set; }
public DbSet<GuidUser.Models.Password> Password { get; set; }
}
Controller.cs:
public class HomeController : Controller
{
private readonly GuidUserContext _context;
public HomeController(GuidUserContext context)
{
_context = context;
}
public IActionResult Index()
{
string name = _context.UserPassword.Select(u=>u.User.Name).First();
return View();
}
}
结果: