开发者

"Update Model from Database" does not see a type change

开发者 https://www.devze.com 2023-04-05 17:47 出处:网络
I have several columns that I changed from Int to BigInt. I opened up my EF model and did an \'Update Model from Database\' and expected to see those columns now be Int64s.But they are still Int32s.

I have several columns that I changed from Int to BigInt.

I opened up my EF model and did an 'Update Model from Database' and expected to see those columns now be Int64s. But they are still Int32s.

(I ran it several time开发者_如何学编程s just to be sure.)

I double checked my database and the columns are definitely BigInts.

So... does 'Update Model from Database' not work for a change of data type? Does it need to be manually applied?


Unfortunately, you'll need to delete the items from your model and then add them back in - at least that's the only thing I have managed to get working.

There is at least one third-party tool that is supposed to help with this, but have not tried it personally.


I'm using VS2008 SP1. If you change the datatype in the "ModelView" (CSDL) of the edmx, errors will occur because the "DatabaseModel" (SSDL) is not updated. You have to edit the *.edmx manually (XML). That is not so hard as it sounds.

You can simply search for the Property that the "Error List" of VS provides you (search in files is maybe the best solution for this). Go to the line where the wrong datatype appear and fix it.

e.g. you changed float to nvarchar(50) on the database --> go to your model and change Double to String --> validate --> Error.... --> Search for the property and make following changes:

      <Property Name="YourChangedProperty" Type="float" />

to

      <Property Name="YourChangedProperty" Type="nvarchar" MaxLength="50" />

This works very well if you know exactly what you've changed on the database. If you've made countless changes, you would have to analyse your changes with some DB-compare tool or regenerate the whole model.

Not very nice. But it "works".

take care M


Correct - data types don't appear to update automatically. You can simply change the data type in the model view using the Properties window and change the Type to Int64.


Answer

For the specific scenario you mentioned, you will need to manually change the Type from Int32 to Int64.

There are a number of ways that this can be done, but the easiest is probably just to open the model (using the default editor) and change the Type of the property from Int32 to Int64.

Explanation (from MSDN Library)

The ADO.NET Entity Data Model Designer (Entity Designer) uses the Update Model Wizard to update an .edmx file from changes made to the database. The Update Model Wizard overwrites the storage model as part of this process. The Update Model Wizard also makes some changes to the conceptual model and mappings, but it only makes these changes when objects are added to the database. For example, new entity types are added to the conceptual model when tables are added to the database, and new properties are added to entity types when columns are added to a table. For details about what changes are made to the .edmx file, see Changes Made to an .edmx File by the Update Model Wizard.

For your scenario, the important thing to note is that the update model wizard is updating the .edmx file with your changes, but only to the storage model. When changes are made to the definition of existing columns, the conceptual model is not updated. For a complete description of changes made by the update model wizard, please see the "Changes Made to an .edmx File by the Update Model Wizard" link above.


You need delete your EF model and than create again and will work.


This may be an older question, but it is most assuredly still relevant today as the issue has not changed. As such, I thought I'd offer a synopsis of the research performed to date on the issue, including some observations of my own:

This failure to update the existing data-types is by design, based upon notes attached to Microsoft's documentation on the topic, as reported in the answer from timb. (Note that Microsoft appears to have either moved or purged the linked document and current documentation does not refer to this issue, but similarly worded notes can still be found elsewhere in archived documentation. Reference) The "Update Model from Database..." wizard does not update those changes, and instead pushes the onus for resolving the issue back onto the developer, in order to avoid making incorrect automatic changes which could theoretically corrupt the Model in ways which were not intended by the developer.

As such, there are really only two ways to resolve the issue, without resorting to third party tools:

  • Delete the affected tables from the Model and re-add them with the "Update Model from Database..." wizard, as noted in the answer from E.J. Brennan. The potential weakness of this method is that it doesn't always succeed, particularly if the original model was generated using a previous version of the Entity Framework, which can sometimes force the developer to perform significantly more work than might otherwise be necessary to complete the task.

  • Manually adjust the affected fields from the graphical model viewer, by right-clicking on the field in the table and selecting "Properties..." from the contextual menu. NOTE: Do not make manual changes directly to the Model .cs files from a code window, as such changes will be reverted the next time the "Update Model from Database..." wizard is run; if the changes are performed from the graphical model viewer, they will persist beyond re-running the wizard.


Also if you work with MySQL on Windows - recreating model may also wont help. Schema is somehow cached in MySQL. So if model is not updated even after recreation try to restart MySQL service and VS to be sure. After that model should be successfully updated.


I solved this by modifying the .EDMX file with a text editor. Find your value and change it's type. Then correct the type in other errors that will be shown in the debugger.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号