Entity Framework AddObject for specific Paste In columns only

We have a system that will use the same code to communicate with various client databases. These databases will use the same EF model, but different connection strings.

Our problem is not that each site will use the same version of our database structure; some may not be in multiple columns or may contain several old columns.

If we upgrade the system to the current version, now the database model now has an additional column EmergencyContact . All old databases will fail because EF is trying to insert into this column (although we have not set a value for this property).

Is there a way to tell EF to use only the columns for which we have a value when it generates an INSERT INTO query?

+4
source share
3 answers

EF will be great if your schema does not have columns that are in the real database, but this will not work if you have columns in the schema that are not in the database, and there is no way to fix this.

Your only choice is to use different schemas for different databases and write code that manages them (i.e. only creates an instance of the context version).

0
source

In the event that your model does not match your database schema, EF will only insert / update columns in the model. However, if the unknown columns are not equal to zero, EF will throw an exception. In addition, if you created relational constraints for unknown columns, then of course they will not be created, because they are not yet known.

0
source

If the persistence level on the site is the only part that changes, I would extract your EF model into my own version, for example.

 DbV1.dll DbV2.dll 

Then you can load into the appropriate DLL based on some parameters from the client, i.e. you could pass the information as a custom header, for example.

 db-version: 1 

There are other more reliable methods, however, I don’t know what your current setting is, so it’s hard to answer.

0
source

Source: https://habr.com/ru/post/1438433/


All Articles