简体   繁体   English

实体框架Fluent API

[英]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. 一个项目将具有调查列表,一个调查将只有一个类别,一个类别可以具有多个调查,SurveyCategory是我存储“调查+类别”链接的表。

Can anyone direct me to what would be appropriate Fluent API code will be for this to map properly.... So far I have this.... 任何人都可以将我定向到适当的Fluent API代码,以便正确地映射它。...到目前为止,我有这个...。

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

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 我希望这有帮助

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

相关问题 与实体框架和流畅的API的代码优先关系 - Code first relationships with entity framework, fluent API 使用fluent api(数据库优先)的1:1关系在Entity Framework中的映射不足 - Insufficient mapping in Entity Framework with fluent api (Database First) on 1:1 relationship 首先具有Fluent API并发性的实体框架代码`DbUpdateConcurrencyException`不引发 - Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising 仅在实体框架6(Fluent Api)中具有外键的表 - Table only with foreign key in entity framework 6 (Fluent Api) 实体框架:如何使用Fluent API实现共享主键? - Entity Framework: how to implement shared primary key with Fluent API? 如何在Entity Framework中使用流利的API为一对一..one关系指定外键属性 - How to specify foreign key property for one to zero..one relation using fluent api in Entity Framework 使用Fluent API映射实体关系-实体框架 - Map Entity Relationships using Fluent APIs - Entity Framework 如何通过Entity Framework中的流利验证将元数据添加到View中的模型中? - How to Add Metadata to model in View by fluent validation in Entity Framework? 实体框架Rest API更改实体 - Entity Framework Rest API Change entity 在Web API中实施实体框架 - Implement the Entity Framework in the Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM