Entity framework ef core如何使用外键同时从不同的表中获取数据?(DB优先)
我的项目中有两个表,“educator”和“educator license” 教育家许可证依赖于具有外键的教育家表。(DB优先) my db中id为的外键: (教育者可以拥有N个许可证) 我的教育模式是:Entity framework ef core如何使用外键同时从不同的表中获取数据?(DB优先),entity-framework,.net-core,entity-framework-core,db-first,Entity Framework,.net Core,Entity Framework Core,Db First,我的项目中有两个表,“educator”和“educator license” 教育家许可证依赖于具有外键的教育家表。(DB优先) my db中id为的外键: (教育者可以拥有N个许可证) 我的教育模式是: public class Educator { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [JsonIgnore] public int id { get; set; }
public class Educator
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonIgnore]
public int id { get; set; }
[JsonIgnore]
public int CompanyId { get; set; }
public string PublicId { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public int Password { get; set; }
public bool Gender { get; set; }
public string AdminNote { get; set; }
public List<EducatorLicense> EducatorLicense { get; set; }
public bool Status { get; set; }
public TimestampsModel Timestamps { get; set; }
}
public class EducatorLicense
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[JsonIgnore]
public int EducatorId { get; set; }
public string LicenseType { get; set; }
}
我的数据库上下文:
protected override void OnModelCreating(ModelBuilder model)
{
model.Entity<Educator>(builder =>
{
builder.ToTable("educators");
builder.Property(p => p.id)
.ValueGeneratedOnAdd();
builder.OwnsOne(c => c.Timestamps,
a =>
{
a.Property(p => p.CreatedAt).HasColumnName("createdAt");
a.Property(p => p.UpdatedAt).HasColumnName("updatedAt");
});
builder.HasOne(d => d.EducatorLicense)
.WithMany()
.HasForeignKey(d => d.Id)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("educatorlicenses_educatorid_foreign");
});
model.Entity<EducatorLicense>(builder =>
{
builder.Property(p => p.id)
.ValueGeneratedOnAdd();
builder.HasKey(c => c.id);
});
}
模型创建时受保护的覆盖无效(ModelBuilder模型)
{
实体(生成器=>
{
建筑商。ToTable(“教育者”);
builder.Property(p=>p.id)
.ValueGeneratedOnAdd();
builder.OwnsOne(c=>c.时间戳,
a=>
{
a、 属性(p=>p.CreatedAt).HasColumnName(“CreatedAt”);
a、 属性(p=>p.UpdatedAt).HasColumnName(“UpdatedAt”);
});
builder.HasOne(d=>d.EducatorLicense)
.有很多
.HasForeignKey(d=>d.Id)
.OnDelete(DeleteBehavior.ClientSetNull)
.HassConstraintName(“教育者许可证”\u educatorid\u foreign”);
});
实体(生成器=>
{
builder.Property(p=>p.id)
.ValueGeneratedOnAdd();
HasKey(c=>c.id);
});
}
我得到了一个例外:
我希望在获取教育家表时使用它获取教育家许可证表
我想我可以在业务层做到这一点,通过使用惰性负载访问数据库两次
有没有一种方法可以让两个表同时连接到另一个表?试着用这个替换数据库上下文
modelBuilder.Entity<Educator>(entity =>
{
entity.HasOne(d => d.Educator)
.WithMany(p => p.EducatorLicences)
.HasForeignKey(d => d.EducatorId)
.OnDelete(DeleteBehavior.ClientSetNull);
});
builder.OwnsOne(c => c.Timestamps,
a =>
{
a.Property(p => p.CreatedAt).HasColumnName("createdAt");
a.Property(p => p.UpdatedAt).HasColumnName("updatedAt");
});
将Educator类的EducatorLicense重命名为EducatorLicenses
public List<EducatorLicense> EducatorLicenses { get; set; }
公共列表教育者许可证{get;set;}
您以错误的方式配置外键。下面的代码-
builder.HasOne(d => d.EducatorLicense)
.WithMany()
.HasForeignKey(d => d.Id)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("educatorlicenses_educatorid_foreign");
应该是-
builder.HasMany(d => d.EducatorLicense)
.WithOne()
.HasForeignKey(d => d.EducatorId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("educatorlicenses_educatorid_foreign");
如果你喜欢的话-
var educatorList = myDbContext.Educators.Include(p=> p.EducatorLicense).ToList();
您将获得一份教育者
列表以及他们的教育者许可证
列表
提示:为了更好地反映它是某事物的集合(因此,关系的
Many
结尾),您应该将Educator
类中的eductorlicense
属性重命名为其复数形式eductorlicenses
var educatorList = myDbContext.Educators.Include(p=> p.EducatorLicense).ToList();