Question: I have two models and first model has a property ICollection. I have one to many relationship between them and I defined that using fluent

I have two models and first model has a property ICollection. I have one to many relationship between them and I defined that using fluent API (using entity framework). But in SQL server I am only seeing first model's table populate with data. The second model's table does not populate as I insert data. Here is more details:

Model 1:

public class Model1{

public int? PersonId { get; set; } public string LastName { get; set; } public string FirstName { get; set; }

public virtual ICollection model2 { get; set; }

}

Model 2:

public class Model2{

public int? Id { get; set; } public int PersonId { get; set; } public int number { get; set; }

}

Fulent API:

modelBuilder.Entity().HasMany(x => x.model2).WithRequired();

I have a feeling I am supposed to put something inside WithRequired()?

why is the table for not populating?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!