0

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);}
tereško
  • 58,060
  • 25
  • 98
  • 150
Anurag
  • 368
  • 4
  • 9

1 Answers1

0

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

Raphael
  • 1,677
  • 1
  • 15
  • 23
  • This is good, but A survey will have only one record in SurveyCategory, so public virtual ICollection SurveyCategory .... I think is not correct. SurveyCategory table will hold ServeryId and CategoryId, one survey will have only one category so in SurveyCategory table I will have just one row for each Survey. I am following Database first approach and I am getting stuck with this, I have to keep the DB structure as is and model it into my application. Does it make sense? – Anurag Nov 06 '13 at 14:05
  • Ok I see your problem now, I had the same issue here at work, I found this template, it creates the model and the Fluent api http://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838, I created a console application where I used it, then I just copied the code that was generated into my project! – Raphael Nov 06 '13 at 15:00
  • You are awesome, this has saved lot of my time, your Reverse POCO suggestion was the solution... – Anurag Nov 07 '13 at 10:49