簡體   English   中英

如何在 C# 中使用外鍵從其他 class 引用 class?

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

我想在 object UserPassword 中有一個帶有 Object User 的字段,它具有 User(uuid,name,rol...) 的所有屬性

用戶密碼代碼:

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

下面是一個工作演示,你可以參考它:

用戶.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 
    }

密碼.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
    }

數據庫上下文.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();
        }
}

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM