EF4未知列字段列表 [英] EF4 Unknown Column In Field List
问题描述
所以,我有点为难。我一直在使用一个通用的存储库,它完美的作品。它坐落在实体框架4.1之上。我用code的同一行获得一组数据多次,以前没有遇到任何问题。然而,在我的数据库这个表似乎是抛出一个异常,我不能为我的生活弄清楚如何解决它。
So, I am kind of stumped. I have been using a generic repository, and it works perfect. It sits on top of Entity Framework 4.1. I have used the same line of code to get a set of data numerous times and had no issues before. However, this one table in my database seems to be throwing an exception and I cannot for the life of me figure out how to fix it.
这是表的设计在MySQL数据库
This is the table design in the MySQL database
Completed
=========
CompletedId
OldStepId
NewStepId
Name
Step
====
StepId
Name
Description
这是model.cs定义
This is the model.cs definition
public class Completed
{
[Key]
public int CompletedId { get; set; }
public int OldStepId { get; set; }
public int NewStepId { get; set; }
public string Name { get; set; }
public virtual Step OldStep { get; set; }
public virtual Step NewStep { get; set; }
}
public class Step
{
[Key]
public int StepId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
上下文定义
public DbSet<Completed> Completeds { get; set; }
public DbSet<Step> Steps { get; set; }
控制器调用code问题
The controller calling code in question
var completeds = new List<Completed>();
using (var gm = new GenericRepo<Completed>())
{
completeds = gm.Get().ToList();
}
库的GET方法(简体)
The get method of the repository (simplified)
public IEnumerable<T> Get()
{
var context = new exampleContext();
DbSet<T> dbSet = context.Set<T>();
IQueryable<T> query = dbSet;
return query.ToList();
}
这是错误导航的动作时,我从浏览器中得到
This is the error I get from the browser when navigating to the action
Unknown column 'Extent1.OldStep_StepId' in 'field list'
这是从调试器获得的内部异常
This is the inner exception obtained from the debugger
InnerException: MySql.Data.MySqlClient.MySqlException
Message=Unknown column 'Extent1.OldStep_StepId' in 'field list'
Source=MySql.Data
ErrorCode=-2147467259
Number=1054
StackTrace:
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
这是一个似乎已经从调试器权问题错误的应用程序崩溃前的查询
This is the query which seems to have the issue from the debugger right before the error crashes the application
{SELECT
`Extent1`.`CompletedId`,
`Extent1`.`OldStepId`,
`Extent1`.`NewStepId`,
`Extent1`.`Name`,
`Extent1`.`OldStep_StepId`,
`Extent1`.`NewStep_StepId`,
`Extent1`.`Step_StepId`,
`Extent1`.`Step_StepId1`
FROM `Completed` AS `Extent1`}
使用反射,我能够确定,或者至少假设,也许StepId没有显示出来,如步骤现场的原因是因为第一步是定义为完成任何原因。这可能不是这样的。我不清楚为什么这个错误发生。
Using reflection I was able to determine, or at least assume, that perhaps the reason StepId is not showing up as a field in Step is because Step is for whatever reason defined as Completed. This may not be the case. I am unsure why this error is occurring.
有没有人遇到过这样的事情实体框架之前?这是我的code某处的一个问题?这是对表的连接方式的一个问题?我有这个设置,其中的工作完美,所以我不明白这里的差异相似的定义。此表和所有其他人之间的唯一区别是,有两个引用相同的对象(注:完成类持有两个虚拟步骤的对象)。
Has anyone encountered something like this with the Entity Framework before? Is this an issue with my code somewhere? Is this an issue of the way the tables are connected? I have similar definitions to this setup which work flawlessly so I do not understand the discrepancy here. The only difference between this table and all the others is that there are two references to the same object (Note: The Completed class holds two virtual Step objects).
另外请注意:这是不会 EF code首先
Also note: this is not EF Code First.
推荐答案
我认为这个问题是因为你还没有配置为使用已定义的FK领域的导航属性。
I think this issue is because you havent configured your navigation property to use the FK field you have defined.
您应该使用:
modelBuilder.Entity<Completed>().HasRequired(e => e.OldStep ).WithMany().HasForeignKey(e => e.OldStepId )
或尝试:
public class Completed
{
[Key]
public int CompletedId { get; set; }
public int OldStepId { get; set; }
public int NewStepId { get; set; }
public string Name { get; set; }
[ForeignKey("OldStepId")]
public virtual Step OldStep { get; set; }
[ForeignKey("NewStepId")]
public virtual Step NewStep { get; set; }
}
public class Step
{
[Key]
public int StepId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
指的http://xhalent.word$p$pss.com/2011/01/21/configuring-entity-framework-4-$c$cfirst/
这篇关于EF4未知列字段列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!