EntitySet System.InvalidOperationException - “the entity type is not part of the model for the current context”

喜欢而已 提交于 2019-11-28 12:21:39
one.beat.consumer

Solved.

The first half was my oversight. The second half... well I don't have a word for what was wrong. It is not really a bug, or incompatibility, but something very inconvenient, intermittent and hard to figure out. First a summary, and then the length explanation for those who care:

Despite the error message suggestions, it was not a problem with the conceptual model (CSDL), but a column mapping issue that recreated itself intermittently.

The conceptual model was built using the EdmxWriter to parse the DbContext and its underlying pieces.

The model then was used to generate SQL scripts to push the schema to a new database. The trick is, the database is Oracle.

Oracle is a baby and does not accept long column names. So the generated EDMX and SQL Scripts had to be modified to build and map parts of the conceptual model to truncated column names.

Not really a big deal. It works fine. So where did things go wrong?

Oracle does not support "code first". And even though it was done manually, using the EdmxWriter constitutes a code-first approach in Oracle's eyes. So when the first EDMX schema was parsed, it bitched about boolean mappings. The solution was to temporarily remove the bools from my C# models, add them to the EDMX manually and make the web.config mapping Oracle suggests (mapping bool to NUMBER(1,0)).

Everything is groovy again. But why does it keep reoccurring?

At different times throughout the development process some ends of the agreement - either C#, EDMX, or Oracle - are altered. And each time, it seems the columns were automatically remapped and I was unaware. If the EDMX model was refreshed from Oracle, the mappings were pointing to C# properties that didn't exist (the short column names). If the model was refreshed from C# code the mappings were not preserved and they attempted to map to long column names that weren't in Oracle.

The bummer with this approach (sort of hybrid code first and model first) is if I want to continue managing my own models and handle customizations necessary for Oracle's little baby attitude, I have to be very careful and monitor the hell out of the EDMX file.

You need to specify mappings for your entities:

public class MealsContext: DbContext
{               
    public MealsContext() : base("ConnectionString")

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // mappings 
    }    

    public DbSet<Meal> Meals{ get; set; }
    public DbSet<Meat> Meats{ get; set; }
    public DbSet<Vegetable> Vegetables { get; set; }
    public DbSet<Drink> Drinks{ get; set; }
}

I had the same problem until I started using Entity Framework Power Tools

Using it you can generate clear entities like a business objects and mapping classes. Good article that helped me to create amazing data access layer: Reverse Engineer Code First

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