Search code examples
asp.net-mvcentity-frameworkone-to-manyfluent-interface

Entity Framework Fluent API


My Entities are as follows...

public class Project{

       public int Id { get; set; }
       public string Name { get; set; }
       public string Description { get; set; }

       public virtual ICollection<Survey> Surveys { get; set; }
}

public class Survey{

       public int Id { get; set; }
       public int ProjectId { get; set; }
       public string Name { get; set; }

       public virtual Project Project { get; set; }

}

public class Category{
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Survey> Surveys { get; set; }
    }

public class SurveyCategory{

        public int Id { get; set; }
        public int SurveyId{ get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }

        public virtual Survey Survey { get; set; }
        public virtual Category Category { get; set; }

}

A project will have list of Surveys, A Survey will have only one Category, A Category can have multiple Survey, SurveyCategory is the table where I am storing Survey + Category link.

Can anyone direct me to what would be appropriate Fluent API code will be for this to map properly.... So far I have this....

 protected override void OnModelCreating(DbModelBuilder modelBuilder){          
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Project>().HasMany(project => project.Surveys);}

Solution

  • hi I hope I understood what you are looking for.

    First of al you should do a few changes to your model

    public class Project
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
    
      public virtual ICollection<Survey> Surveys { get; set; }
    } 
    public class Survey
    {
      public int Id { get; set; }
      public int ProjectId { get; set; }
      public int CategoryId { get; set; }
      public string Name { get; set; }
    
      public virtual Project Project { get; set; }
      public virtual Category Category { get; set; }
      public virtual ICollection<SurveyCategory> SurveyCategory { get; set; }
    }
    public class Category
    {
      public int Id { get; set; }
      public string Name { get; set; }
    
      public virtual ICollection<Survey> Surveys { get; set; }
      public virtual ICollection<SurveyCategory> SurveyCategory { get; set; }
    }
    public class SurveyCategory
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public int SurveyId { get; set; }
      public virtual Survey Survey { get; set; }
      public int CategoryId { get; set; }
      public virtual Category Category { get; set; }
    }
    

    And then on the model creating you should do this

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Project>()
                        .HasMany(p => p.Surveys)
                        .WithRequired(u => u.Project);
            modelBuilder.Entity<Category>()
                        .HasMany(p => p.Surveys)
                        .WithRequired(u => u.Category);
            modelBuilder.Entity<Category>()
                        .HasMany(p => p.SurveyCategory)
                        .WithRequired(u => u.Category)
                        .HasForeignKey(k => k.CategoryId)
                        .WillCascadeOnDelete(false);
            modelBuilder.Entity<Survey>()
                       .HasMany(p => p.SurveyCategory)
                       .WithRequired(u => u.Survey)
                       .HasForeignKey(k => k.SurveyId)
                       .WillCascadeOnDelete(false);
        }
    

    I hope this helps