Home > Software engineering >  How to reference a class from other class with foreign key in C#?
How to reference a class from other class with foreign key in C#?

Time:03-09

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...)

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:

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 :

string name = userPassword.User.Name;

CodePudding user response:

A work demo is below,you can refer to it:

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();
        }
}

result:

enter image description here

  • Related