【问题标题】:Entity Framework Fluent API实体框架 Fluent API
【发布时间】:2013-11-06 10:35:48
【问题描述】:

我的实体如下...

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

}

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

谁能告诉我什么是合适的 Fluent API 代码才能正确映射....到目前为止我有这个....

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

【问题讨论】:

    标签: asp.net-mvc entity-framework one-to-many fluent-interface


    【解决方案1】:

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

    首先你应该对你的模型做一些改变

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

    希望对你有帮助

    【讨论】:

    • 这很好,但是A调查在SurveyCategory中只有一条记录,所以公共虚拟ICollection SurveyCategory ....我认为是不正确的。 SurveyCategory 表将包含 ServeryId 和 CategoryId,一项调查将只有一个类别,因此在 SurveyCategory 表中,每个调查只有一行。我正在遵循数据库优先方法,但我遇到了这个问题,我必须保持数据库结构不变并将其建模到我的应用程序中。有意义吗?
    • 好的,我现在看到你的问题了,我在工作中遇到了同样的问题,我找到了这个模板,它创建了模型和 Fluent api visualstudiogallery.msdn.microsoft.com/…,我创建了一个控制台应用程序,并在其中使用它,然后我只是将生成的代码复制到我的项目中!
    • 你太棒了,这节省了我很多时间,你的反向 POCO 建议是解决方案......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2014-08-04
    • 1970-01-01
    • 2011-05-29
    相关资源
    最近更新 更多