2

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

Glory Raj
  • 17,397
  • 27
  • 100
  • 203

2 Answers2

1

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

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Many thanks for detailed explanation, for these two model mappings i do not need write any mappings externally under db context right – Glory Raj Nov 20 '19 at 03:50
  • Right. Even if you don't use migrations, you can always `Add-Migration` to create temporary migration and see (inside `Up` method) how EF Core maps your entity model to database (and `Remove-Migration` when you are done). – Ivan Stoev Nov 20 '19 at 09:05
0

try this

  modelBuilder.Entity<sections>()
 .HasOne(a => a.Requests)
Raj
  • 385
  • 2
  • 15
  • I am looking kind of one to many relationship.. there will be more sections in requests table like i shown in the requests table.. – Glory Raj Nov 20 '19 at 02:40