asp.net mvc - Inserting multiple records in the third table having foreign keys to parent using entity framework 6

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

Question

I am trying to learn how to use Entity Framework 6 with an already created database, without creating an .edmx file, i.e, using the DbContext and POCO classes.

These are my model classes:

[Table("Category")]
public class Category
{
   [Key]
   public long CategoryID { get; set; }
   public string CategoryName { get; set; }
}

[Table("RegistrationForm")]
public class RegistrationForm
{
    [Key]
    public int RegistrationID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; } 
    public int Country { get; set; }        
    public string Email { get; set; }       
    public string Phone { get; set; }  
}   

[Table("RegistrationCategory")]
public class RegistrationCategory
{
    [Key]
    public long RegistrationCategory { get; set; }
    public long RegistrationID { get; set; }//Foreign key to RegistrationID in RegistrationForm table in database
    public long CategoryID { get; set; }//Foreign key to CategoryID in Category table in database
}

My DbContext class:

public class MyContext : DbContext
{
    public virtual DbSet<RegistrationForm> RegistrationForm { get; set; }
    public virtual DbSet<Category> Category { get; set; }
    public virtual DbSet<RegistrationCategory> RegistrationCategory { get; set; }
}

Here I want to use the default model builder of DbContext.User can select multiple categories in the registration screen so the RegistrationCategory table will have multiple records for each registration. Therefore RegistrationForm and RegistrationCategory are in a one-to-many relationship.

How to write foreign key mappings between the above mentioned models?

How to bind data from Category table data in the mvc view(listbox) so that we can save one record in RegistrationForm table and multiple records in RegistrationCategory table without using loops (using mappings between the c# models) in Entity Framework 6?

1
0
3/15/2015 9:00:33 AM

Accepted Answer

The database schema that you have here is a Many to Many relationship between RegistrationForm and Category, with a join table. The RegistrationCategory Table is not necessary to be modeled in Entity Framework at all. You will need to use Entity Framework Fluent API to generate the correct mappings.

First, your RegistrationForm Table:

public class RegistrationForm
{
    [Key]
    public int RegistrationID { get; set; }
    ...

    // add a navigation property ICollection<Category> to reference the categories
    public virtual ICollection<Category> Categories { get; set; }
}  

Next, the Category class:

public class Category
{
    [Key]
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }

    //Navigation property to reference the RegistrationForms
    public virtual ICollection<RegistrationForm> RegistrationForms { get; set; }
}

next, in your DbContext: note the change in pluralization, and the removal of the RegistrationCategory, you do not need a model class for it at all.

public class MyContext : DbContext
{
    public virtual DbSet<RegistrationForm> RegistrationForms { get; set; }
    public virtual DbSet<Category> Categories { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<RegistrationForm>()
        .HasMany(r => r.Categories)
        .WithMany(c => c.RegistrationForms)
        .Map(
            m =>
            {
              m.MapLeftKey("RegistrationID");
              m.MapRightKey("CategoryID");
              m.ToTable("RegistrationCategory");
            }
        );
}

With this in place, you can now query all the Categories of a RegistrationForm or all the RegistrationForms of a Category.

foreach (var category in registrationForm.Categories)
{
    //do whatever with each category
}
1
3/15/2015 9:24:37 AM


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