Search code examples
c#entity-frameworkentity-framework-coreef-code-firstef-code-first-mapping

getting an error if mapping is not there with EF core code first


Hi all i have model sections like this below

public class Sections
{
    public int SectionId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public  Requests Requests { get; set; }
}

the structure of the data for sections model like this below

 sectionId      Name      description
      1         code1       code 1
      2         code2       code 2

i have one more model Requests and the model looks like this below

public class Requests
{
    public int RequestId { get; set; }
    public string  Description { get; set; }
    public int SectionId { get; set; }
    public  Sections sections { get; set; }
}

and the structure of sample data for Requests model like this below

RequestId   Description   SectionId 
    1          test1         1
    2          test2         1
    3          test1         2
    4          test2         2

with this structure of model data i am mapping these two models below

  modelBuilder.Entity<Requests>()
     .HasOne(a => a.sections)
     .WithOne(o => o.Requests); //not sure if this is correct way to map these two models with one-to-many mapping as listed in requests model

is that above mentioned mapping is correct way to achieve the same and I am using Entity framework core code first approach.

if i don't use above mapping i am getting this error:

The child/dependent side could not be determined for the one-to-one relationship between Requests.sections and Sections.Requests.

Could any one please let me know if there is any other way to map those two models


Solution

  • The sample data for Requests shows that the relationship between Section and Request is one-to-many (for instance, there are 2 requests with SectionId == 1).

    So the current reference navigation property

    public Requests Requests { get; set; }
    

    which implies one-to-one should become collection navigation property

    public ICollection<Requests> Requests { get; set; }
    

    Now you can use HasOne + WithMany or HasMany + WithOne to configure the relationship. But that most likely won't be necessary because usually EF Core can conventionally determine the one-to-many relationships.

    So while not strongly mandatory, it would be better to use singular names for entities and reference navigation properties, and plural names for collection navigation properties:

    public class Section
    {
        public int SectionId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public ICollection<Request> Requests { get; set; }
    }
    
    public class Request
    {
        public int RequestId { get; set; }
        public string  Description { get; set; }
        public int SectionId { get; set; }
        public Section Section { get; set; }
    }
    

    This way you will be following EF Core conventions and in most of the cases won't need data annotations / fluent configuration.

    Reference: Relationships