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

1 回答 1

0

嗨,我希望我明白你在找什么。

首先,您应该对模型进行一些更改

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

然后在创建模型上你应该这样做

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

我希望这有帮助

于 2013-11-06T12:15:13.247 回答