简体   繁体   中英

I want to add AspNetUsers table "Id" column as Foreign Key in another table

ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<Hospital> Hospitals { get; set; }        


        protected override void OnModelCreating(ModelBuilder builder)
        {
            
        }       
    }

Hospital.cs

public class Hospital : Common
    {
        [Key]
        [Column(Order = 1)]
        public int HospitalId { get; set; }

        [Required]
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Required]        
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Invalid e-mail address.")]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Contact Person")]
        public string ContactPerson { get; set; }

        [Required]
        [Display(Name = "Phone Number")]
        [DataType(DataType.PhoneNumber)]
        public string PhoneNum { get; set; }

        [Required]
        [MinLength(10)]
        [Display(Name = "Mobile Number")]
        public string MobileNum { get; set; }

        [Required]
        [Display(Name = "Address")]        
        public string Address { get; set; }
        
        [Required]
        [Display(Name = "Short Name")]
        public string ShortName { get; set; } //Unique Slug name        
    }

Common.cs

public class Common
    {
        public DateTime? CreatedOn { get; set; }        
        public string CreatedBy { get; set; }
        public DateTime? EditedOn { get; set; }        
        public string EditedBy { get; set; }
        public DateTime? DeletedOn { get; set; }        
        public string DeletedBy { get; set; }
        public bool? IsDeleted { get; set; } = false;
        public bool? IsActive { get; set; } = true;        
    }

I want to add the primary key of the AspNetUsers table in the Hospital table as foreign key with these columns:

  1. CreatedBy

  2. EditedBy

  3. DeletedBy

Main Goal: So using this kind of relationship I want to display the User Name with Hospital details , who is Create/Edit/Delete hospital details.

Configure the relasionship using the data annotation: in Hospital class add the following attribute:

public int AspNetUsersId{ get; set; }

[ForeignKey("AspNetUsersId")]
public virtual Common aspNetUser { get; set; }

and in Common class add the Hospital Attribute:

public virtual Hospital hospital { get; set; }

Now, you should define this relation in ApplicationDvContext:

protected override void OnModelCreating(ModelBuilder builder)
{
     builder.Entity<Hospital>()
            .HasOptional(s => s.aspNetUser) 
            .WithRequiredPrincipal(ad => ad.hospital);    
}  

I don't recommend between two table more than one relation. I think, There's no need forgein relations for these colums. Bcs these colums're just information coll. But still you can edit common class like this.

 public class Common
    {
        public DateTime? CreatedOn { get; set; }
        [ForeignKey("AspNetUsers")] public string CreatedBy { get; set; }
        public AspNetUsers CreatedByUser { get; set; }
        public DateTime? EditedOn { get; set; }
        [ForeignKey("AspNetUsers")] public string EditedBy { get; set; }
        public AspNetUsers EditedByUser { get; set; }
        public DateTime? DeletedOn { get; set; }
        [ForeignKey("AspNetUsers")] public string DeletedBy { get; set; }
        public AspNetUsers DeletedByUser { get; set; }
        public bool? IsDeleted { get; set; } = false;
        public bool? IsActive { get; set; } = true;

    }

You should configure your database from a management program with the key-relationships you want and then update the entity data model from your solution, otherwise Entity won't be able to find the relationship by itself. If you are using microsoft sql management studio check this article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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