实体框架5 - 无效的列名称 - 逆向工程师代码第一 [英] Entity Framework 5 - Invalid column name - Reverse Engineer Code First
问题描述
这是我的数据库表结构:
我使用上述扩展名的逆向工程师代码优先功能,它生成了POCO类和一些映射文件(不知道这是否是正式的说明)和一个单独的DbContext派生类。除了下面描述的变化之外,所有这些生成的类都是由电动工具生成的。
在Category.cs文件中,我添加了以下代码帮助平铺对象图:
private ICollection< Product> m_Products = null;
public ICollection< Product>产品
{
get
{
if(m_Products == null)
{
m_Products = new List< Product>();
foreach(var categoryProduct in CategoryProducts)
{
m_Products.Add(categoryProduct.Product);
}
}
返回m_Products;
}
set {m_Products = value; }
}
我得到以下例外,我知道必须与映射,但我只是不能很清楚这一点。
未处理的异常:System.Data.EntityCommandExecutionException:发生错误而
执行命令定义。查看内部例外情况。
---> System.Data.SqlClient.SqlException:
列名Category_CategoryId无效。
如果我需要发布更多信息,例如映射的具体细节,只需让我知道我会做的。我想保持尽可能的短,但我意识到我已经省略了一些事情,对于那些不熟悉该工具生成的代码的人来说,可能会留下更多的细节。
您已将导航属性添加到模型中,因此EF正在尝试将其映射到数据库。 Code First表示您的代码模型定义了您的数据库模式。
尝试将 [NotMapped]
属性添加到您的帮助人员告诉EF忽略它们。
Using Entity Framework 5, Visual Studio 2010 with the Entity Framework Power Tools (Beta 2) extension.
Here is my database table structure:
I used the Reverse Engineer Code First function of the aforementioned extension, which generated the POCO classes and some 'mapping' files (not sure if that's the formal parlance) and a single DbContext-derived class. Other than the change I describe next, all of these generated classes are as-generated by the power tool.
In the Category.cs file, I added the following code to help flatten the object graph a bit:
private ICollection<Product> m_Products = null;
public ICollection<Product> Products
{
get
{
if (m_Products == null)
{
m_Products = new List<Product>();
foreach (var categoryProduct in CategoryProducts)
{
m_Products.Add(categoryProduct.Product);
}
}
return m_Products;
}
set { m_Products = value; }
}
I get the following exception, which I know must have something to do with the mappings, but I just can't quite figure this out.
Unhandled Exception: System.Data.EntityCommandExecutionException: An error occurred while
executing the command definition. See the inner exception for details.
---> System.Data.SqlClient.SqlException:
Invalid column name 'Category_CategoryId'.
If I need to post more information, such as the specifics of the mappings, just let me know and I'll do it. I wanted to keep this as short as possible, but I realize I've omitted some things that, for those unfamiliar with the code generated by the tool, may leave one wanting for more details.
You've added a navigation property to your model and so EF is trying to map that to your database. "Code First" means your code model defines your database schema.
Try adding the [NotMapped]
attribute to your helper properties to tell EF to ignore them.
这篇关于实体框架5 - 无效的列名称 - 逆向工程师代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!