繁体   English   中英

实体框架Fluent API

[英]Entity Framework Fluent API

我的实体如下...

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

}

一个项目将具有调查列表,一个调查将只有一个类别,一个类别可以具有多个调查,SurveyCategory是我存储“调查+类别”链接的表。

任何人都可以将我定向到适当的Fluent API代码,以便正确地映射它。...到目前为止,我有这个...。

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

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

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

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

我希望这有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM