简体   繁体   English

如何获取实体框架以查询正确的表?

[英]How do I get Entity Framework to query the right table?

I've got a class defined 我定义了一个班级

public class ReportClass
{
    public int ID { get; set; }
    public int ClassIndex { get; set; }
    public string ClassName { get; set; }
    public int CompanyID { get; set; }

}

and I set up a dbcontext. 并且我设置了dbcontext。

public class ReportClassContext : DbContext
{
    public DbSet<ReportClass> ReportClasses { get; set; }
}

When I first went to get records, the runtime tells me the database table doesn't exist: I check, and I see that the name of my DbSet doesn't match with the table. 当我第一次去获取记录时,运行时告诉我数据库表不存在:我检查了一下,发现DbSet的名称与该表不匹配。 I switched the name to match: 我将名称切换为匹配:

public class ReportClassContext : DbContext
{
    public DbSet<ReportClass> ReportClassesRealTable { get; set; }
}

but it is still querying against the non-existent table. 但它仍在查询不存在的表。

What am I doing wrong? 我究竟做错了什么?

Use the table attribute like this: 使用表属性,如下所示:

[Table("ReportClassesRealTable")]
public class ReportClass
{
    public int ID { get; set; }
    public int ClassIndex { get; set; }
    public string ClassName { get; set; }
    public int CompanyID { get; set; }

}

This tells the EF what the actual table name is for your class, otherwise it attempts to use the plural form of your class name. 这会告诉EF您的类的实际表名是什么,否则它将尝试使用类名的复数形式。

Let this be there as it is 让它原样存在

public DbSet<ReportClass> ReportClasses { get; set; }

Now overrde the OnMoedlCreateing method to tell EF to map this class to a different table using fluent API . 现在覆盖OnMoedlCreateing方法,以告诉EF使用流畅的API将该类映射到其他表。 Add that method to your DBContext class 将该方法添加到您的DBContext类中

public class ReportClassContext : DbContext
{
    public DbSet<ReportClass> ReportClasses { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Entity<ReportClass>().ToTable("ReportClassesRealTable");
    }
}

This tells EF that when you query ReportClasses property of your DbContxt object, It will fetch data from teh ReportClassRealTable table in your database. 这告诉EF,当您查询DbContxt对象的ReportClasses属性时,它将从数据库中的ReportClassRealTable表中获取数据。

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

相关问题 实体框架 - 如何获取列? - Entity Framework - how do I get the columns? 在使用带有实体框架的Table-per-Type时,如何仅获取基表行? - How do I get just the base table rows when using Table-per-Type with Entity Framework? 如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体? - How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0? 在实体框架中,您将如何对联接表进行“ IN”查询? - In the entity framework, how would you do an “IN” query on a joined table? 如何使用Entity Framework获取存储在上下文中的任意实体? - How do I get an arbitrary entity stored in the context with Entity Framework? 如何在实体框架中进行“in”查询? - How to do an “in” query in entity framework? 如何在 C# 实体框架中获取 SQL 查询的结果? - How do I get the result of SQL Query in C# Entity Framework? 如何使用LINQ和实体框架6进行表联接? - How do I do a table join using LINQ and entity framework 6? 数据库中已删除的表。 如何获得实体框架以进行重塑? - Deleted table in database. How do I get entity framework to remake it? 如何在此Entity Framework IQueryable查询中抽象出select? - How do I abstract out the select in this Entity Framework IQueryable query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM