【问题标题】:I want to add AspNetUsers table "Id" column as Foreign Key in another table我想在另一个表中添加 AspNetUsers 表“Id”列作为外键
【发布时间】:2022-01-01 00:01:41
【问题描述】:

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)
        {
            
        }       
    }

医院.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;        
    }

我想在 Hospital 表中添加 AspNetUsers 表的主键作为外键,其中包含以下列:

  1. 创建者

  2. 编辑者

  3. 删除者

主要目标:因此,使用这种关系,我想显示 User Name with Hospital details,即创建/编辑/删除医院详细信息。

【问题讨论】:

    标签: c# entity-framework-core foreign-keys ef-code-first


    【解决方案1】:

    我不建议在两个表之间建立一个以上的关系。我认为,这些列没有必要建立关系。 Bcs这些colums只是信息coll。但是你仍然可以像这样编辑普通类。

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

    【讨论】:

    • 我添加了您编写的代码,但它会引发错误实体类型“IdentityUserLogin”需要定义主键。如果您打算使用无密钥实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无密钥实体类型的更多信息,请参阅 go.microsoft.com/fwlink/?linkid=2141943 您可以在此处查看 link
    【解决方案2】:

    使用数据注释配置关系: 在 Hospital 类中添加以下属性:

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

    并在 Common 类中添加医院属性:

    public virtual Hospital hospital { get; set; }
    

    现在,你应该在 ApplicationDvContext 中定义这个关系:

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

    【讨论】:

    • HasRequired 抛出错误。看这张图片:link
    • 对不起,它是 HasOptional 不是 HasRequired
    【解决方案3】:

    您应该使用您想要的键关系从管理程序配置您的数据库,然后从您的解决方案更新实体数据模型,否则实体将无法自行找到关系。如果你使用的是微软sql管理工作室check this article.

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2019-07-29
      相关资源
      最近更新 更多