ASP.NET - 如何使用实体框架进行内部联接

时间:2018-05-24 18:11:10

标签: asp.net entity-framework

我正在使用ASP.NET MVC和Entity Framework,我使用SqlQuery获取数据:

dbBlog.Data.SqlQuery("SELECT bc.id, bc.title, bc.post, bc.date, bc.featuredImage, cc.category FROM BlogClasses bc INNER JOIN CategoriesClasses cc ON bc.category = cc.id WHERE cc.category = '" + id.Replace("_", " ") + "' ORDER BY bc.date desc").ToList();

我现在正在尝试做什么,并且在不使用SqlQuery的情况下是INNER JOIN,我已经对Join进行了一些阅读,我真的很难理解它。

我定义了我的类别:

private Categories dbCategories = new Categories();

我真的不知道下一步该怎么做以及.join如何运作,有人能指出我正确的方向吗?

谢谢,

2 个答案:

答案 0 :(得分:0)

在不知道您的架构的情况下,我无法给出具体的答案,但是Join(),您需要提供:

IEnumerable<TInner> inner - 这就是你想要加入

Expression<Func<TOuter, TKey>> outerKeySelector - 这是如何从中加入的任何内容中指定字段。

Expression<Func<TInner, TKey>> innerKeySelector - 这是您指定要加入的字段的方式 - 来自之前定义的TInner

Expression<Func<TOuter, TInner, TResult>> resultSelector - 这是您将从加入中返回的内容。

示例,此SQL查询:

SELECT i.*
FROM Outer o JOIN Inner i ON o.SomeId = i.SomeId
WHERE o.SomeValue < 5

可以用Join()编写,如:

var result = outers.Where(o => o.SomeValue < 5)
                 .Join(inners, o => o.SomeId, i => i.SomeId, (o, i) => i);

答案 1 :(得分:0)

实体框架使用配置来反映相关实体之间的关系。鉴于以下实体:

public class BlogClass
{
   public int Id {get; set;}
   public string Title {get; set;}
   public string Post {get; set;}
   public DateTime Date {get; set;}
   public string FeaturedImage {get; set;} // Assuming a URL?

   public virtual ClassCategory Category {get;set;}
}

public class ClassCategory
{
   public int Id {get; set;}
   public string Category {get; set;}
   // Other properties...
}

我们可以设置我们的配置:

public class BlogClassConfiguration : EntityTypeConfiguration<BlogClass>
{
   public BlogClassConfiguration()
   {
      ToTable("BlogClasses");
      HasKey(x => x.Id)
         .Property(x => x.Id)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); // Assuming the ID's are set up as identity or have defaults.

      HasRequired(x => x.Category)
         .WithMany()
         .Map(x => x.MapKey("Category")); // This maps our CategoryClasses reference (Category) to a column on the BlogCategories table called "Category".
   }
}
Public class ClassCategoryConfiguration : EntityTypeConfiguration<ClassCategory>
{
   public ClassCategoryConfiguration()
   {
      ToTable("CategoryClasses");
      HasKey(x => x.Id)
         .Property(x => x.Id)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
   }
}

我们告诉DbContext在构建模型时加载这些配置:

public class BlogDbContext : DbContext
{
   public DbSet<BlogClass> BlogClasses {get; set;}

   public BlogDbContext(string connectionString)
      : base (connectionString)
   {}

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
      base.OnModelCreating(modelBuilder);

 modelBuilder.Configurations.AddFromAssembly(typeof(TestDbContext).Assembly);
   }
}

然后EF完全管理连接。

var categoryName = category.Replace("_", " ");

using (var context = new BlogContext("BlogConnectionString"))
{
   var blogs = context.BlogClasses
      .Where(x => x.Category.Category == categoryName)
      .OrderByDescending(x => x.Date)
      .ToList(); // Assumes not too many items returned!
}

此示例将DbContext范围作为示例。理想情况下,这应该由IoC容器管理,并作为依赖项提供。