Entity framework 使用实体框架Core 3.1在OnModelCreating中定义一对多关系
我对EntityFrameworkCore3.1还不熟悉,并试图定义两个表之间的一对多关系。我目前正在努力,并得到编译错误。谁能告诉我问题出在哪里 错误是: PersonNote不包含PersonNote的定义 我现在正在排队Entity framework 使用实体框架Core 3.1在OnModelCreating中定义一对多关系,entity-framework,entity-framework-core,Entity Framework,Entity Framework Core,我对EntityFrameworkCore3.1还不熟悉,并试图定义两个表之间的一对多关系。我目前正在努力,并得到编译错误。谁能告诉我问题出在哪里 错误是: PersonNote不包含PersonNote的定义 我现在正在排队 entity.HasOne(d => d.PersonNote) 我还能如何定义一对多关系 这两个表是Person和PersonNote。一个人可以有许多人。我已经为他们定义了模型 public class Person { public int
entity.HasOne(d => d.PersonNote)
我还能如何定义一对多关系
这两个表是Person
和PersonNote
。一个人可以有许多人。我已经为他们定义了模型
public class Person
{
public int Id { get; set; }
public int? TitleId { get; set; }
public string FirstName { get; set; }
public string FirstNamePref { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public string Gender { get; set; }
public int AddressId { get; set; }
public string TelephoneNumber { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public int? PartnerId { get; set; }
public bool Enabled { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
public string ModifiedBy { get; set; }
public DateTime Modified { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordStartDateTime { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordEndDateTime { get; set; }
public Address Address { get; set; }
public Title Title { get; set; }
public Client Client { get; set; }
internal static IEnumerable<object> Include(Func<object, object> p)
{
throw new NotImplementedException();
}
public PersonNote PersonNote { get; set; }
}
public class PersonNote
{
public int Id { get; set; }
public int PersonId { get; set; }
public string Note { get; set; }
public int AuthorId { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordStartDateTime { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordEndDateTime { get; set; }
}
public IEnumerable<PersonNote> GetPersonNotes(int personId)
{
var PersonNotes = PersonNote
.Include(x => x.)
.Where(x => x.Id == personId)
.ToList();
return PersonNotes;
}
您应该具有如下内容(省略了其他属性):
班级人员
{
[关键]
公共int Id{get;set;}
公共列表PersonNotes{get;set;}
}
班级人事说明
{
[关键]
公共int Id{get;set;}
公共int PersonId{get;set;}
}
类StackOverflow:DbContext
{
公共DbSet Persons{get;set;}
公共DbSet PersonNotes{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasMany(p=>p.PersonNotes)
.WithOne()
.HasForeignKey(p=>p.PersonId);
}
}
您的实体是PersonNote
(实体
)-它没有PersonNote
属性。应该有Person
navigation属性。我添加了,但上面提到的错误仍然是public Person Person{get;set;}嗨,Johny错误似乎已经消失了,但是我的方法GetPersonNotes需要返回特定personid的personnotes。我无法在includes中添加Person实体。我尝试在personnote public Person{get;set;}内部静态IEnumerable Include(Func p){throw new NotImplementedException();}中添加以下内容,但这似乎不起作用。建立关系的整个想法是能够从中返回结果query@Tom如果您需要特定人员的注释,请按这些人员筛选并包括他们的注释。例如:context.Persons.Where(p=>p.Id==10)。Include(p=>p.PersonNotes)
我实际上是在按个人查询personnotes表,而不是相反。@Tom,Include
Methodin person不属于这里Include
是一种内置的实体框架方法。但你现在正在扩展你原来的问题。这样做效果不好,应该避免。你的问题得到了回答。如果您有新问题,请开始新问题。
modelBuilder.Entity<PersonNote>(entity =>
{
entity.ToTable("PersonNote", "common");
entity.HasOne(d => d.PersonNote)
.WithMany(p => p.Person)
.HasForeignKey(d => d.PersonId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_commonPersonNote_commonPerson");
});
class Person
{
[Key]
public int Id { get; set; }
public List<PersonNote> PersonNotes { get; set; }
}
class PersonNote
{
[Key]
public int Id { get; set; }
public int PersonId { get; set; }
}
class StackOverflow : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<PersonNote> PersonNotes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasMany(p => p.PersonNotes)
.WithOne()
.HasForeignKey(p => p.PersonId);
}
}