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

Without building a new database, I'm attempting to learn how to use Entity Framework 6 with one that has already been developed..edmx making use of theDbContext and seminars at POCO.

My sample classes are as follows:

[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; }
}

I want to utilize the built-in model builder ofDbContext In the registration screen, the user has the option to choose from numerous categories.RegistrationCategory Each registration will have a number of records in the table. ThereforeRegistrationForm and RegistrationCategory who in aone-to-many relationship.

How should foreign key mappings be created between the models indicated above?

How to bind information fromCategory data tables in themvc view (listbox ), allowing us to save one record inRegistrationForm table with numerous records inRegistrationCategory table in Entity Framework 6 without using loops (using mappings between the C# models)?

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

Accepted Answer

The link between the tables in this database is many to many.RegistrationForm and Category utilizing a join table. TheRegistrationCategory Entity Framework does not require that a table be modelled in any way. To create the proper mappings, you must utilize Entity Framework Fluent API.

your firstRegistrationForm 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; }
}  

Then, theCategory 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; }
}

after that, in your DbContext, make the following changes: note the pluralization is changed, and theRegistrationCategory , you don't even require a model class for it.

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");
            }
        );
}

Now that this is established, you can query all theCategories of aRegistrationForm or everyRegistrationForms of aCategory .

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