msawczyn / EFDesigner

Entity Framework visual design surface and code-first code generation for EF6, Core and beyond
MIT License
363 stars 60 forks source link

Partial classes being deleted on regeneration #318

Open Relki opened 2 years ago

Relki commented 2 years ago

Discussed in https://github.com/msawczyn/EFDesigner/discussions/317

Originally posted by **Relki** July 21, 2022 I'm adding partial classes to the generated Context file. I've tried putting the partial class in the same folder as the generated class. I've tried putting it in a different folder, but adjacent. But, when I regenerate the designer code from the EF designer, it regenerates the generated code, but then deletes my custom partial context class. The only way I've been able to have the partial context class not be deleted, is to put it in the root of the project level. Then it doesn't get deleted by the code generator. Any clues as to what I'm doing wrong? Is this a bug? **Generate code deletes the AccountModelContext.Encrypted.cs file in both these scenarios.** ![image](https://user-images.githubusercontent.com/45949455/180154940-d5063e03-e7bb-4d12-880c-c93616995982.png) ![image](https://user-images.githubusercontent.com/45949455/180155875-e57a839f-cba0-4095-88c4-07bc60525561.png) **But doesn't delete it in this scenario.** ![image](https://user-images.githubusercontent.com/45949455/180156194-04fed9ed-bd85-4e78-81df-d19a2d19300e.png) Generated code: ![image](https://user-images.githubusercontent.com/45949455/180155046-11cd89d9-6891-42d5-9e8b-2e6b3585c4c4.png) My Partial class for the same context; ![image](https://user-images.githubusercontent.com/45949455/180155188-161eb07b-c4ff-4871-b9e6-283ca6379808.png)
Relki commented 2 years ago

Also, when I use dotnet ef migrations add InitialCreate to create migrations, it creates a folder called "Migrations" in the root of the EF project. But when I click save on the efModel, just like the partial classes, it goes through and deletes the migration and snapshot files that dotnet ef creates.

It seems like the generating code walks through and just deletes any code files in any folders. I just tried today to move the model efmodel file and model.tt file into a sub folder instead of the root folder, and then tried same process again. Still deletes any partial classes I create and any migrations created.

Is there a recommended way to create migrations with this designer extensions? I haven't found anything in the Wiki talking to this.

Next I'm going to just tell dotnet ef to generate migration files in the root folder of EF. Maybe then the generated designer code will ignore them.

This designer seems to make using migrations not possible with default documentation from Microsoft. Going to try pointing to root folder today and will have to accept that as a work around for now if it works.

msawczyn commented 2 years ago

I've never seen this behavior before. Can you provide a small sample solution where you see the problem occur?

Relki commented 2 years ago

I've pulled the Entity Framework projects out of my solution, built a separate solution with my Entity Framework project set up that reproduces this issue.

Note, this project is using .Net 7.0 now. It was using .Net 6.0 and the issue reproduced with that framework as well.

When you open the solution, take note that I have put the partial classes in the same folder as the other Entities. Example;

Versona.EF\Models\TableEntities\Account.cs Versona.EF\Models\TableEntities\Account.ShouldSerialize.cs

Account.cs is auto generated, but Account.ShouldSerialize.cs is my partial class implementation. There are several other entities in the same folder with the same naming format.

Also, same pattern with the DbContext;

Versona.EF\Models\Contexts\VersonaModelContext.cs Versona.EF\Models\Contexts\VersonaModelContext.Implemented.cs

Now, to reproduce the issue;

  1. Open the solution with Visual Studio 2022 (latest with .Net 7 SDK you'll likely need).
  2. Navigate to the Versona.EF project
  3. Double click on file Models\VersonaModel.efmodel
  4. Right click on background and click "Generate Code".
  5. You'll notice that every single partial class will be deleted from the project (Under Models\Contexts and Models\TableEntities).

The only way I could prevent this from happening, was to move all those partial classes, to the root project level. Then they don't get deleted whenever code is generated.

Now, as a note, I did try creating a new project and adding Visual Entity Framework to it again, created a simple Entity with 1 table, and then created a partial class for that same table in the same folder as the auto generated code. When I generated code, it did not delete the partial classes.

So, I could not reproduce this with a brand new project with just 1 table entity. But it does reproduce in my larger project with many entities. I have two such projects, with many entities (Account.EF and Versona.EF). Both larger projects have the exact same issue reproduced.

I had the same problem as well if I created a folder in Versona.EF project called "Migrations". I use to have all my migrations under that folder. However, when the code was generated, it also deleted all the migration class files as well. Imagine my surprise. I had to either move my migration files to the root folder or move them to a separate project (which I ended up doing).

DemoRepro.zip

Relki commented 1 year ago

Was the repro helpful?