简体   繁体   English

我想在另一个表中添加 AspNetUsers 表“Id”列作为外键

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

ApplicationDbContext.cs 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医院.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 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:我想将Hospital表中AspNetUsers表的主键添加为具有这些列的外键

  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:使用数据注释配置关系:在医院 class 添加以下属性:

public int AspNetUsersId{ get; set; }

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

and in Common class add the Hospital Attribute:在 Common class 中添加医院属性:

public virtual Hospital hospital { get; set; }

Now, you should define this relation in ApplicationDvContext:现在,您应该在 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. Bcs这些colums只是信息coll。 But still you can edit common class like this.但是您仍然可以像这样编辑常见的 class 。

 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.您应该使用您想要的键关系从管理程序配置您的数据库,然后从您的解决方案更新实体数据 model,否则实体将无法自行找到关系。 If you are using microsoft sql management studio check this article.如果您使用的是微软 sql 管理工作室,请查看这篇文章。

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

相关问题 AspNetUsers的主键作为另一个表的外来 - Primary key of AspNetUsers as foreign of another table AspNetUsers的ID作为单独表中的外键,一对一的关系 - AspNetUsers' ID as Foreign key in separate table, one-to-one relationship 身份新表外键AspNetUsers - Identity new table foreign key to AspNetUsers EF代码优先MVC:我应该将字段添加到默认的AspNetUsers表中还是创建另一个与AspNetUsers表相关的“ UserInfo”表? - EF Code-first MVC: Should I add fields to the default AspNetUsers table or create another 'UserInfo' table that relates to the AspNetUsers table? 如何在现有AspNetUsers表中添加列 - How to add a column in an existing AspNetUsers table 将标识列添加到 AspNetUsers 表 MVC Core - Add Identity column to AspNetUsers table MVC Core 如何使用ASP.NET MVC中该表的另一列获取外键表中的记录ID? - How do I get the id of record in foreign key table using another column in that table in ASP.NET MVC? 如何将AspNetUser表ID作为外键映射到另一个表 - How to map AspNetUser table id as foreign key to another table 如何使用 EF 6 在另一个表中为外键添加列描述? - How do you add column description for a foreign key in another table utilizing EF 6? 我应该将外键表存储为Entity Framework中的外键ID还是该表的C#模型? - Should I store foreign key table as foreign key ID or the C# model of that table in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM