Defining the one to many relationship in OnModelCreating using Entity Framework Core 3.1

entity-framework entity-framework-core

Question

I am new to Entity Framework Core 3.1 and trying to define the one-to-many relationship between two tables. I am currently struggling and getting compilation errors. Could somebody tell me what the problem could be.

The error is:

PersonNote does not contain the definition for PersonNote

I am currently getting is at line

entity.HasOne(d => d.PersonNote)

How else could I define one-to-many relationship?

The two tables are Person and PersonNote. One Person can have many PersonNotes. I have defined the models for them

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

I have tried the following in OnModelCreating:

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");
            });
1
0
1/4/2020 8:13:31 PM

Accepted Answer

You should have have something like this (other properties are omitted):

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);
    }
}
0
1/4/2020 7:49:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow