Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Partial classes being deleted on regeneration #318

Open
Relki opened this issue Jul 21, 2022 Discussed in #317 · 4 comments
Open

Partial classes being deleted on regeneration #318

Relki opened this issue Jul 21, 2022 Discussed in #317 · 4 comments
Labels
investigating Looking into this need info Need more information to track this down

Comments

@Relki
Copy link

Relki commented Jul 21, 2022

Discussed in #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
image

But doesn't delete it in this scenario.
image

Generated code:
image

My Partial class for the same context;
image

@Relki
Copy link
Author

Relki commented Jul 26, 2022

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
Copy link
Owner

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

@msawczyn msawczyn added investigating Looking into this need info Need more information to track this down labels Aug 14, 2022
@Relki
Copy link
Author

Relki commented Nov 18, 2022

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
Copy link
Author

Relki commented Dec 2, 2022

Was the repro helpful?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
investigating Looking into this need info Need more information to track this down
Projects
None yet
Development

No branches or pull requests

2 participants