天道酬勤,学无止境

Column Extent1.Id does not exist in Mono with Entity Framework 6 and PostgreSQL

I am trying to map to a PostgreSQL database with Entity Framework using the packages Npsql and Npsql.EntityFramework. I have a database with the following table:

CREATE TABLE IF NOT EXISTS Crops (
  Id            INT PRIMARY KEY     NOT NULL,
  Name          TEXT                NOT NULL,
  SowingMonths  INT
);

The Crop model looks as follows:

public class Crop
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Month SowingMonths { get; set;} //Month is an enum
}       

In the database context I map the schema to public since all tables are in the schema public by default in PostgreSQL.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("public");
}

Now when I open the view that tries to access Crops of the DbContext I get this message:

Npgsql.NpgsqlException
ERROR: 42703: column Extent1.Id does not exist
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): Npgsql.
Exception stack trace:
at Npgsql.NpgsqlState+d__0.MoveNext () [0x00000] in :0 at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject (Boolean cleanup) [0x00000] in :0

The good news is the database can find the table now (it could not at first, but it can with the mapping to the public schema in the OnModelCreating method). However, it cannot find the column name Extend.Id.

Why it tries to look for Extend.Id is beyond me. We have defined the property on the Crops model as Id, why then does it look for Extend.Id? But considering this is the case, how can I get this to work? Can I just create a database table with a property Extend1.Id instead of Id? And if so, how do I do it?

Thank you!

评论

I'm convinced that there is a typographical error somewhere in your code. The table "Id" simply does not exist. Capital letters maybe?

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • Error in LINQ when work with PostGreSQL by Entity Framework
    I have a problem with LINQ query. Here this: List<a> get = (from i in entitiesFactory.subsDbContext.a where i.id == id select i).ToList<a>(); The model look like this" public partial class a { [Key] public int id { get; set; } [ForeignKey("contractors"), Column(Order = 0)] public Nullable<int> ot_contractor_id { get; set; } [ForeignKey("contractors1"), Column(Order = 1)] public Nullable<int> gvo_contractor_id { get; set; } public virtual contractors contractors { get; set; } public virtual contractors contractors1 { get; set; } } Table a look like this: CREATE TABLE a ( id serial NOT NULL, ot
  • 通过实体框架使用 PostGreSQL 时出现 LINQ 错误(Error in LINQ when work with PostGreSQL by Entity Framework)
    问题 我有 LINQ 查询问题。 在这里: List<a> get = (from i in entitiesFactory.subsDbContext.a where i.id == id select i).ToList<a>(); 模型长这个样子” public partial class a { [Key] public int id { get; set; } [ForeignKey("contractors"), Column(Order = 0)] public Nullable<int> ot_contractor_id { get; set; } [ForeignKey("contractors1"), Column(Order = 1)] public Nullable<int> gvo_contractor_id { get; set; } public virtual contractors contractors { get; set; } public virtual contractors contractors1 { get; set; } } 表看起来像这样: CREATE TABLE a ( id serial NOT NULL, ot_contractor_id integer, gvo_contractor_id integer
  • Entity Framework 6 - inheritance and navigation properties on base class
    I have a problem with navigation properties and inheritance. This is my problem: I have a base Person class and classes User and Worker which inherit from Person. On the DB level I'm using single table inheritance or table per hierarchy (TPH) inheritance. So there a single table with a discriminator column. Both User and Worker need to have a Company relation, so I would like to define it on the Person class. I define my model like this: [Table("mydb.person")] public abstract partial class Person { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ID { get; set; } public
  • 实体框架 6 - 基类的继承和导航属性(Entity Framework 6 - inheritance and navigation properties on base class)
    问题 我有导航属性和继承的问题。 这是我的问题:我有一个基本的Person类以及继承自Person类User和Worker 。 在 DB 级别,我使用单表继承或表每个层次结构 (TPH) 继承。 所以有一个带有鉴别器列的表。 User和Worker需要有Company关系,所以我想在Person类上定义它。 我这样定义我的模型: [Table("mydb.person")] public abstract partial class Person { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long ID { get; set; } public long? CompanyID { get; set; } [ForeignKey("CompanyID")] public virtual Company Company { get; set; } ... } public partial class User : Person { ... } public partial class Worker : Person { .... } [Table("mydb.company")] public partial class Company { public Company() { this
  • 如何在实体框架中查询空值?(How can I query for null values in entity framework?)
    问题 我想执行这样的查询 var result = from entry in table where entry.something == null select entry; 并生成一个IS NULL 。 编辑:在前两个答案之后,我觉得有必要澄清一下我使用的是Entity Framework,而不是Linq to SQL。 object.Equals()方法似乎在EF中不起作用。 编辑2号:上述查询按预期方式工作。 它正确生成IS NULL 。 我的生产代码是 value = null; var result = from entry in table where entry.something == value select entry; 并且生成的SQL是something = @p; @p = NULL something = @p; @p = NULL 。 似乎EF可以正确转换常量表达式,但是如果涉及到变量,它将像对待普通比较一样对待它。 实际上是有道理的。 我将结束这个问题。 回答1 Linq-to-SQL的解决方法: var result = from entry in table where entry.something.Equals(value) select entry; Linq到实体的解决方法(哎呀!): var result = from entry
  • Linq To Entity - Any VS First VS Exists(Linq To Entities - Any VS First VS Exists)
    问题 我正在使用实体框架,我需要检查名称 = "xyz" 的产品是否存在...... 我想我可以使用 Any()、Exists() 或 First()。 哪一种是这种情况的最佳选择? 哪个性能最好? 谢谢你, 米格尔 回答1 好的,我不打算对此进行权衡,但迭戈的回答使事情变得足够复杂,我认为需要一些额外的解释。 在大多数情况下, .Any()会更快。 这里有些例子。 Workflows.Where(w => w.Activities.Any()) Workflows.Where(w => w.Activities.Any(a => a.Title == "xyz")) 在上面的两个示例中,实体框架生成了一个最优查询。 .Any()调用是谓词的一部分,实体框架可以很好地处理这个问题。 但是,如果我们将.Any()部分的结果作为这样的结果集: Workflows.Select(w => w.Activities.Any(a => a.Title == "xyz")) ... 突然间,实体框架决定创建两个版本的条件,因此查询所做的工作是它真正需要的工作的两倍。 但是,以下查询也好不到哪里去: Workflows.Select(w => w.Activities.Count(a => a.Title == "xyz") > 0) 鉴于上述查询,实体框架仍将创建两个版本的条件,此外它还需要
  • 已经有一个与此连接关联的打开的 DataReader,必须先关闭它 + asp.net mvc(There is already an open DataReader associated with this Connection which must be closed first + asp.net mvc)
    问题 我有与它的多个领域,如ENTITY_TITLE,entity_description表entites的MySQL数据库,...。 在表中还有 3 个外键user_id , region_id和category_id 。 在我的索引视图中,我想显示表中的所有实体(显示标题、描述、...、用户名、区域名称和类别名称)。 这就是我在我的控制器中所做的: public ActionResult Index() { var model = this.UnitOfWork.EntityRepository.Get(); return View(model); } 在我的存储库中,我这样做: public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = _dbSet; if (filter != null) { query = query.Where(filter); } foreach (var
  • Entity Framework async operation takes ten times as long to complete
    I’ve got an MVC site that’s using Entity Framework 6 to handle the database, and I’ve been experimenting with changing it so that everything runs as async controllers and calls to the database are ran as their async counterparts (eg. ToListAsync() instead of ToList()) The problem I’m having is that simply changing my queries to async has caused them to be incredibly slow. The following code gets a collection of "Album" objects from my data context and is translated to a fairly simple database join: // Get the albums var albums = await this.context.Albums .Where(x => x.Artist.ID == artist.ID)
  • 实体框架查询速度很慢,但是SqlQuery中相同的SQL速度很快(Entity Framework query slow, but same SQL in SqlQuery is fast)
    问题 我看到一些非常奇怪的性能,它们与使用.NET Framework版本4的Entity Framework Code-First进行的非常简单的查询有关。LINQ2Entities查询如下所示: context.MyTables.Where(m => m.SomeStringProp == stringVar); 这需要3000毫秒以上的时间才能执行。 生成的SQL看起来非常简单: SELECT [Extent1].[ID], [Extent1].[SomeStringProp], [Extent1].[SomeOtherProp], ... FROM [MyTable] as [Extent1] WHERE [Extent1].[SomeStringProp] = '1234567890' 通过Management Studio运行时,该查询几乎立即运行。 当我将C#代码更改为使用SqlQuery函数时,它将在5到10毫秒内运行: context.MyTables.SqlQuery("SELECT [Extent1].[ID] ... WHERE [Extent1].[SomeStringProp] = @param", stringVar); 因此,使用完全相同的SQL,在两种情况下,生成的实体都会进行更改跟踪,但是两者之间的性能差异很大。 是什么赋予了? 回答1 找到了。
  • Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table?
    i'm trying do some Entity Framework Code First programming to an existing database .. but I keep seeing this code in my Sql Profiler :- SELECT TOP ( 1 ) [Extent1].[Id] AS [Id], [Extent1].[ModelHash] AS [ModelHash] FROM [dbo].[EdmMetadata] AS [Extent1] ORDER BY [Extent1].[Id] DESC What the hell is this EdmMetadata table and why do is my EF code trying to grab the Id and ModelHash from there? Remember, I'm trying to work against an existing DB. Cheers :)
  • Get max & min from Entity Framework, in one query and with best query possible
    I'm aware of this question, but what I would like to do is obtain something close to this generated SQL: select MAX(Column), MIN(Column) from Table WHERE Id = 1 When I try this: var query = from d in db.Table where d.Id == 1 select new { min = db.Table.Max(s => s.Column), max = db.Table.Min(s => s.Column) }; The generated sql looks like this: SELECT [Extent1].[Id] AS [Id], [GroupBy1].[A1] AS [C1], [GroupBy2].[A1] AS [C2] FROM [dbo].[Table] AS [Extent1] CROSS JOIN (SELECT MAX([Extent2].[Column]) AS [A1] FROM [dbo].[Table] AS [Extent2] ) AS [GroupBy1] CROSS JOIN (SELECT MIN([Extent3].[Column])
  • Comparing performance of generated queries for Any() vs Count() in Entity Framework 5
    In my project I use Entity Framework 4.4.0.0 and I have the following dilemma. I have to check if an user is activated. My query looks like: Any() _context.Users.Any(u => u.Id == userId && u.IsActivated); The generated sql is: SELECT CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent1] WHERE ( [Extent1].[Id] = @p__linq__0 ) AND ( [Extent1].[IsActivated] = 1 )) ) THEN cast(1 AS BIT) WHEN ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent2] WHERE ( [Extent2].[Id] = @p__linq__0 ) AND ( [Extent2].[IsActivated] = 1 )) ) THEN cast(0 AS BIT) END AS [C1] FROM (SELECT 1 AS X)
  • 比较实体框架 5 中 Any() 与 Count() 生成查询的性能(Comparing performance of generated queries for Any() vs Count() in Entity Framework 5)
    问题 在我的项目中,我使用 Entity Framework 4.4.0.0,但我遇到了以下困境。 我必须检查用户是否被激活。 我的查询看起来像: 任何() _context.Users.Any(u => u.Id == userId && u.IsActivated); 生成的sql为: SELECT CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent1] WHERE ( [Extent1].[Id] = @p__linq__0 ) AND ( [Extent1].[IsActivated] = 1 )) ) THEN cast(1 AS BIT) WHEN ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent2] WHERE ( [Extent2].[Id] = @p__linq__0 ) AND ( [Extent2].[IsActivated] = 1 )) ) THEN cast(0 AS BIT) END AS [C1] FROM (SELECT 1 AS X) AS [SingleRowTable1] 对于Count()我得到这个查询: SELECT [GroupBy1].[A1] AS [C1] FROM
  • 使用现有的主从数据库在 EF 上选择错误(Select error on EF using existing master detail database)
    问题 Entity Framework v5 看起来很酷,我尝试使用现有的MSSQL (Azure) 数据库从 Linq-to-SQL 切换。 但是关于 EF 的教程太复杂了,无法遵循。 数据库模式非常简单,如下(由exist db 生成)。 Sheets和SheetDetails表之间是一对多的关系。 CREATE TABLE [dbo].[Sheets] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Grade] FLOAT (53) CONSTRAINT [DF_Sheets_Grade] DEFAULT ((0)) NOT NULL, [Title] NVARCHAR (255) CONSTRAINT [DF_Sheets_Title] DEFAULT ('') NOT NULL, [Description] NVARCHAR (255) NULL, [Difficulty] SMALLINT CONSTRAINT [DF_Sheets_Difficulty] DEFAULT ((0)) NOT NULL, [Writer] NVARCHAR (255) CONSTRAINT [DF_Sheets_Writer] DEFAULT ('') NOT NULL, [Tag] NVARCHAR (255) NULL, [Duration] FLOAT
  • Table Does Not Exist while using EF 6 and Oracle.ManagedDataAccess
    I am creating a MVC application using EF 6.0.0.0 and ODP.Net Oracle.ManagedDataAccess version 4.121.2.0 for the data access. In my Controller called EmployeeController, I have the following code snippet: public ActionResult Details(int id) { try { EmployeeContext employeeContext = new EmployeeContext(); Employee employee = employeeContext.Employees.Single(x => x.Id == id); //Here the exception occurs! return View(employee); } catch (Exception e) { return View(e); } } And when I load the Employee/Details.cshtml page I got the following Exception: "An error occurred while executing the command
  • 使用 EF 6 和 Oracle.ManagedDataAccess 时表不存在(Table Does Not Exist while using EF 6 and Oracle.ManagedDataAccess)
    问题 我正在使用 EF 6.0.0.0和 ODP.Net Oracle.ManagedDataAccess版本4.121.2.0创建一个 MVC 应用程序以进行数据访问。 在我的名为EmployeeController Controller ,我有以下代码片段: public ActionResult Details(int id) { try { EmployeeContext employeeContext = new EmployeeContext(); Employee employee = employeeContext.Employees.Single(x => x.Id == id); //Here the exception occurs! return View(employee); } catch (Exception e) { return View(e); } } 当我加载Employee/Details.cshtml页面时,出现以下异常: “执行命令定义时发生错误。有关详细信息,请参阅内部异常。” 在内部异常中,它说: ORA-00942: 表或视图不存在 这让我感到困惑,因为在我的 Oracle 数据库中,该表肯定存在(我使用 Toad for Oracle 进行了检查): 该connectionString数据库本身是我使用的其他项目
  • Force Entity Framework to use SQL parameterization for better SQL proc cache reuse
    Entity Framework always seems to use constants in generated SQL for values provided to Skip() and Take(). In the ultra-simplified example below: int x = 10; int y = 10; var stuff = context.Users .OrderBy(u => u.Id) .Skip(x) .Take(y) .Select(u => u.Id) .ToList(); x = 20; var stuff2 = context.Users .OrderBy(u => u.Id) .Skip(x) .Take(y) .Select(u => u.Id) .ToList(); the above code generates the following SQL queries: SELECT TOP (10) [Extent1].[Id] AS [Id] FROM ( SELECT [Extent1].[Id] AS [Id], row_number() OVER (ORDER BY [Extent1].[Id] ASC) AS [row_number] FROM [dbo].[User] AS [Extent1] ) AS
  • How to optimize Entity Framework Queries
    I am using Linq-To-Entities to do a query which is returning only 947 rows but taking 18 seconds to run. I have done a "ToTraceString" to get the underlying sql out and ran the same thing directly on the database and get the same timing. I have used the tuning advisor and created a couple of indexes although with little impact. Looking at the query execution plan there are a couple of nested loops which are taking up 95% of the time but these are already working on the indexes? Does anyone have any ideas on how to force some optimisation into the EF query?? EDIT: Supplying additional
  • Entity Framework 何时触发 SQL 命令?(When exactly does Entity Framework Fire a SQL Command?)
    问题 假设我有这样的代码: public void TestWhenSqlFires() { var db = new Db(); // EF DbContext var items = db.SimpleObjects; // SimpleObject implements interface IId var byId = WhereId(items, 1); var array = byId.ToArray(); } protected IEnumerable<IId> WhereId(IEnumerable<IId> items, int id) { return items.Where(i => i.Id == id); } SQL 将在 TestWhenSqlFires() 的哪一行实际针对数据库运行? (这是一个从对此答案的评论中分离出来的问题) 回答1 自己找出并测试的一种方法: 打开 SQL Server Management Studio,打开一个新查询,选择 EF 将运行的数据库并运行此查询: SELECT top 10 deqs.last_execution_time AS [Time], dest.TEXT AS [Query] FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql
  • Select error on EF using existing master detail database
    Entity Framework v5 looks cool and I try to switch from Linq-to-SQL with existing MSSQL (Azure) database. but tutorials about EF are too complex to follow. The database schema is pretty simple as follows (generated by exist db). Sheets and SheetDetails tables are connected with 1-to-many relationship. CREATE TABLE [dbo].[Sheets] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Grade] FLOAT (53) CONSTRAINT [DF_Sheets_Grade] DEFAULT ((0)) NOT NULL, [Title] NVARCHAR (255) CONSTRAINT [DF_Sheets_Title] DEFAULT ('') NOT NULL, [Description] NVARCHAR (255) NULL, [Difficulty] SMALLINT CONSTRAINT [DF_Sheets