简体   繁体   English

如何在 C# 中使用外键从其他 class 引用 class?

[英]How to reference a class from other class with foreign key in C#?

I would like to have in the object UserPassword a field with a Object User,that has all the attributes of User(uuid,name,rol...)我想在 object UserPassword 中有一个带有 Object User 的字段,它具有 User(uuid,name,rol...) 的所有属性

Code of UserPassword:用户密码代码:

 [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; }

Code of 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 List<UserPassword> userPassword { get; set; }

Code of DBContext: 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; }

The goal is to can extract an User from the UserPassword class, like this:目标是可以从 UserPassword class 中提取用户,如下所示:

string name = userPassword.User.Name;

A work demo is below,you can refer to it:下面是一个工作演示,你可以参考它:

User.cs:用户.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:密码.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:数据库上下文.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: 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();
        }
}

result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM