Entity Framework 5 - Invalid column name - Reverse Engineer Code First

回眸只為那壹抹淺笑 提交于 2019-12-12 13:24:24

问题


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.


回答1:


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.




回答2:


In case you've created DB scheme automatically and you are not using strategies like (DropDatabaseAlways/DropDatabaseIfModelChanges) - other words: you are really in Reverse Engineering, it seems that you have to manually add column "CategoryId" on "Category" table.

In case, you don't want to work with the property (I mean in DB), you can use Data Annotation [NotMapped] or Fluent API modelBuilder.Entity<Category>().Ignore(x=> x.CategoryId)

Finally it is possible that problem can be in mapping. I don't know whether you are using data annotations or Fluent API but EF may automatically looks for some db column (logical behavior derived from the model) and can not find it. In this case I recommend you make a revision of the mapping.




回答3:


The OP already solved their problem, but I've had a similar error with a different solution. So here it is in case others need it:

I was missing a navigation property on one side of a 0..1 relationship between entities. Once I added an appropriate navigation property to the entity that was missing it, the problem was solved.

A bit more details: I had two entities with a 0..1 relationship using a FK. Entity A (parent) had a FK to Entity B (child). The child entity B had a navigation property to entity A, but A did not have a navigation property to B. After adding this, the problem was solved.



来源:https://stackoverflow.com/questions/14142186/entity-framework-5-invalid-column-name-reverse-engineer-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!