Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Insert data in Join table which were created by using EF Code First
    primarykey
    data
    text
    <p>I have two Entity classes defined as </p> <p>1) Project Class</p> <pre><code>public class Project { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int ProjectId { get; set; } public string Title { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public ICollection&lt;Person&gt; Persons { get; set; } } </code></pre> <p>2) Person Class</p> <pre><code> public class Person { public Person() { Projects = new List&lt;Project&gt;(); } [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int PersonId { get; set; } public string Name { get; set; } public string EmailAddress { get; set; } public string Designation { get; set; } public string CellPhone { get; set; } public ICollection&lt;Project&gt; Projects { get; set; } } </code></pre> <p>And in my DataContext Class, the configuration is defined as</p> <pre><code> public class DataContext: DbContext { public DataContext() : base("name=DataContext") { } public DbSet&lt;Project&gt; Projects { get; set; } public DbSet&lt;Person&gt; Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;Project&gt;() .HasMany&lt;Person&gt;(p =&gt; p.Persons) .WithMany(p =&gt; p.Projects) .Map(m =&gt; { m.ToTable("ProjectPersons"); m.MapLeftKey("ProjectId"); m.MapRightKey("PersonId"); }); } } </code></pre> <p>After I created new project The project table and Person table populate with it's record but I notice ProjectPerson table which were created through joining not populated with the ProjectId And PersonId.</p> <p>Any suggestion please?</p> <p>Here is my HttpPost method to create new project</p> <pre><code>[HttpPost] public ActionResult CreateProject(Projects newProject, int page = 1) { _ctx.Projects.AddProject(newProject, ref _ctx); _ctx.Person.AddPerson(newProject.Person, newProject, ref _ctx); _ctx.SaveChanges(); var userProjects = _ctx.Projects.GetStaffUserProjects(providerUserKey, page, ref _ctx); return PartialView("_Projects", userProjects); } </code></pre> <p>And here are my Extension method which 'AddProject' and 'AddPerson'</p> <p>1) AddProject</p> <pre><code>public static void AddProject(this IEnumerable&lt;Project&gt; projects, Projects newProject, ref DataContext ctx) { var project = new Project { Title = newProject.Title, Duration = newProject.Duration, Description = newProject.Description, CreatedDate = DateTime.Now, Status = true, UserId = (int)Membership.GetUser().ProviderUserKey, ProjectCategoryId = newProject.SelectedProjectCategoryId, OrganizationId = newProject.SelectedOrganizationId }; ctx.Projects.Add(project); } </code></pre> <p>2) AddPerson</p> <pre><code>public static void AddPerson(this IEnumerable&lt;Person&gt; persons, Person newPerson, Projects newProject, ref DataContext ctx) { var person = new Person { Name = newPerson.Name, EmailAddress = newPerson.EmailAddress, CellPhone = newPerson.CellPhone, Designation = newPerson.Designation }; ctx.Person.Add(person); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload