codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.88k stars 353 forks source link

Partial method must have an implementation part because it has accessibility modifiers. #962

Open CarryoutRepo opened 1 year ago

CarryoutRepo commented 1 year ago

Environment

Description

Entity framework creates a partial void from its template for DbContext that reads...

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

...which CodeMaid adds "private" to like this...

private partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

...which changes the auto-generated code and prevents any builds.

There is no way to fix it if you have CodeMaid on Save on without leaving VS.

Awesome product, THANKS!!!!

Steps to recreate

  1. Create a class with the problem... internal partial class Class1 { partial void OnModelCreatingPartial(int x = 1); }

  2. Run CodeMaid on the class. It will now read... internal partial class Class1 { partial void OnModelCreatingPartial(int x = 1); }

  3. Now the class can't compile because.... CS8795 Partial method 'Class1.OnModelCreatingPartial(int)' must have an implementation part because it has accessibility modifiers.

Current behavior

Adds "private" to a partial void.

Expected behavior

Code maid should not add an accessibility modifier for partial voids.

Maira-Ashfaq commented 1 year ago

thanks this solution is really helpful i forget i use codemaid extension

codecadwallader commented 1 year ago

Thanks for reporting the issue. I have not been able to reproduce it. We do have explicit bypass logic for the partial keyword on classes but I don't know that we have the same on methods.

asichi commented 1 year ago

Love CodeMaid. I have the same issue as the OP.

For now, I have excluded the dbcontext file from code maid cleanup to solve the problem.

tombohub commented 1 year ago

@asichi how do you exclude?

asichi commented 1 year ago

@tombohub Extensions | CodeMaid | Options | Cleaning | File Types | Exclude

GrantHair5 commented 1 year ago

Can confirm this is happening for me also.

image

image

@asichi absolute lifesaver haha ❤️

pauldbentley commented 1 year ago

I have the same issue on an old project using LinqToSql.

the Designer.cs file generates partial methods on the entities:

partial void OnLoaded();

Then if I implement this partial in another class:

partial void OnLoaded()
{
    // do something
}

This gets changed to

private partial void OnLoaded()
{
    // do something
}

Resulting in error CS8799 - Both partial method declarations must have identical accessibility modifiers.

rozatoo commented 1 year ago

Experiencing this issue too, ty @asichi 👍

ahaleiii commented 1 year ago

I am also experiencing this after using EF Core to scaffold my database context.

It generates:

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

In my case, I think I can comment out this method and its reference, but I will need to do this every time I re-scaffold.

hellow554 commented 9 months ago

Hey @codecadwallader

you said you cannot reproduce this and tagged it as such. Unfortunalty many people (include me) encounter this problem.

Would you be able to investigate this problem again, please?

I use the mvvm toolkit and it the same with changing methods (https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#running-code-upon-changes)

bjornmicallef commented 9 months ago

Can confirm I have experienced the above issue too and the easiest, quickest fix was to exluce the file like @asichi suggested. Thank you

fangfsz commented 7 months ago

I got the same issue as @hellow554 .

hwarang098tkd commented 2 months ago

same . I Excluded it

giaenuuu commented 2 weeks ago

I'm not keen on excluding an entire file. Instead, a better solution might be to disable the automatic insertion of access modifiers on methods.

CodeMaid → Options → Cleaning → Insert → "Insert explicit access modifiers for" → methods

I prefer to disable it this way to ensure that the rest of the file remains clean. Just remember to set your accessibility modifiers from now on. If you already do this like I do, you'll be perfectly fine.