0

The Visual Studio integrated tool provided by Microsoft is fine but it's not enough for some rare scenarios in which we just want to add new tables (generate EDMX & C# model classes for just those new tables) to the existing entity model but not want to refresh the whole entity model (because the current EDMX is fairly much out of sync with what in the database and changing it a lot means testing is required a lot and I don't want that change).

It could have been an easy feature but looks like the only scenario they thought of here is a complete syncing (perfect mapping) between code model & database.

We can code to create such a tool but it requires fairly lot of efforts (regarding full understanding about the EDMX schema, the mapping rules, ...).

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Hopeless
  • 4,397
  • 5
  • 37
  • 64
  • EDMX was abandoned more than 10 years ago, fully replaced by either scaffolding a database directly to code or generating a database from code. Adding a table to the model now means just adding the class and running migrations – Panagiotis Kanavos Aug 26 '22 at 08:11
  • @PanagiotisKanavos I know it's almost dead but I have to maintain a legacy project still using EDMX, of course migrating is a good solution but still it's not an option now (requiring much efforts to migrate & test). – Hopeless Aug 26 '22 at 08:12
  • The double mappings required by EDMX were *hated* by developers and never picked up by tool vendors. The intermediary model was meant to be The One Model that multiple tools would use to generate ORM mappings, reports, web services etc, but nobody beyond the EF designer used it. Developers ended up having to maintain a model *and* duplicate mappings just to map their tables to classes – Panagiotis Kanavos Aug 26 '22 at 08:14
  • @PanagiotisKanavos I hated EDMX about 10 years ago as well and basically I hate all legacy stuffs =)) but having to maintain a legacy project is something sometimes I can't avoid. I doubt that there is some third-party tool or package to solve this but I do hope that there is such a package/tool ***provided by Microsoft*** that I've not known of. At least I can base on that to create my own tool more easily. – Hopeless Aug 26 '22 at 08:19
  • `not an option now` neither is what you try to do now. The model is out of sync, so you can't perform model operations any more. It's like asking to query a database using tables or columns that no longer exist. I'm not even sure if the model is even used in EF 6 or it just generates the classes and DbContext configurations used in normal operations. You may be able to just add a table, entity class and DbSet property without ever touching the model. You may even be able to delete it entirely – Panagiotis Kanavos Aug 26 '22 at 08:24
  • @PanagiotisKanavos the model is a standard EF6 database-first model, it requires all the mapping info to be added to the EDMX file correctly, so it's not simple as just define model classes to use. The hard part is to add mapping info to the EDMX file from what in the database. Currently I have to use the integrated tool to generate the mappings normally but have to manually discard all the other unrelated changes (due to the `model refreshing` auto-done by the tool when updating the model from db). – Hopeless Aug 26 '22 at 08:29
  • `create my own tool more easily` that could be an XML parser that reads the model, picks the table element you want and generates SQL and code from it. Or you could generate code and models in a *different* project and use only the parts you need. You said it yourself, the model is out of sync so it can't be used to edit tables and entities any more. – Panagiotis Kanavos Aug 26 '22 at 08:29
  • 1
    EF 6 database-first means no EDMX, that the entities and DbContext are scaffolded from the command-line. EDMX is only involved in model-first. In any case, you may be able to use ErikEJ's [EF 6 Power Tools](https://github.com/ErikEJ/EntityFramework6PowerTools) at least as a starting point. They allow you to read and visualize the model and customize the reverse engineer templates. – Panagiotis Kanavos Aug 26 '22 at 08:32
  • @PanagiotisKanavos actually the tool need to read schemas from db and generate the XML data (or update to an existing EDMX) but of course for only new tables (without trying to sync anything else). My scenario here is not editing existing tables, just add new tables. – Hopeless Aug 26 '22 at 08:32
  • If the model matches the code you'll be able to create the table in the model, generate a *new* database, and copy the differences to the original database. SSMS and Azure Data Studio have Schema Comparison that can generate a SQL script with the differences – Panagiotis Kanavos Aug 26 '22 at 08:36
  • @PanagiotisKanavos yes but I usually design the tables first directly by using SQL Server Management Studio, I feel it's easier that way. What you suggest is kind of model-first. – Hopeless Aug 26 '22 at 08:39
  • If the model is out of sync, you don't have many options. Either fix the model, or try hacks for as long as possible. Another thing you can do is create a new project, reverse engineer the database or at least a subset, and copy the differences to the old project. – Panagiotis Kanavos Aug 26 '22 at 08:44
  • @PanagiotisKanavos that's what I've tried :), I create new tables in a completely new database and generate EDMX & model classes from that. But then manual copying still requires fairly much effort. I had an idea to try automating this process somehow but at least it need to be run as a tool (like cmd ...), the UI tool requires fairly much user interaction. – Hopeless Aug 26 '22 at 08:48

1 Answers1

0

In the case where the model and the database are completely out of sync, it is more convenient to save the manual copying process, and add the mapping information from the content in the database to the EDMX file.

I am sorry that I have not found a similar software package in Microsoft that implements the described function/tool.

Lei Zhang-MSFT
  • 280
  • 1
  • 5
  • could you explain more about this phrase `it is more convenient to save the manual copying process, and add the mapping information from the content in the database to the EDMX file` ? I don't understand it well. I have to use the only tool (integrated into VS) provided by Microsoft and that tool does not work the way I want (as already explained in my question). Thanks! – Hopeless Sep 07 '22 at 04:04