klaasjan69
klaasjan69

Reputation: 191

How to avoid program exception when ef model changes?

I use a third party database with Entity Framework 6. This works fine; however, when a table within my model is changed (three columns were deleted), my program throws an exception:

System.Data.SqlClient.SqlException: Invalid column name '<deleted column>'

I don't use any of these columns. I only read them from the database. I can update my model, but then when there is another change in a table, my program will crash again. How can I modify my program so that it won't crash on the next database change?

Upvotes: 0

Views: 271

Answers (2)

klaasjan69
klaasjan69

Reputation: 191

  1. Create a context with only the entities you need. Create entities with only the properties you need. see EF code first.
  2. Use Fluent API to specify primary keys and more.

It will still crash though if any of your entities/properties gets changed/deleted

Upvotes: 0

bubi
bubi

Reputation: 6491

You can use a Code First approach starting from database (generate classes from database). At the end of class generation you can delete entities that you don't need (i.e. entities related to all unused tables) or properties related to unused fields.
Disable migrations.
You can also delete intermediate files generated by EF code generation (files different from .cs files).
At this point, any changes to database that not affects mapped classes/properties does not cause errors in EF.

Upvotes: 1

Related Questions