codemaid icon indicating copy to clipboard operation
codemaid copied to clipboard

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

Open CarryoutRepo opened this issue 2 years ago • 15 comments

Environment

  • Visual Studio Microsoft Visual Studio Professional 2022
  • CodeMaid version: 12.0
  • Code language: C#

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.

CarryoutRepo avatar Dec 13 '22 16:12 CarryoutRepo

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

Maira-Ashfaq avatar Dec 29 '22 18:12 Maira-Ashfaq

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.

codecadwallader avatar Jan 14 '23 18:01 codecadwallader

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.

asichi avatar Jan 29 '23 14:01 asichi

@asichi how do you exclude?

tombohub avatar Feb 26 '23 17:02 tombohub

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

asichi avatar Feb 26 '23 20:02 asichi

Can confirm this is happening for me also.

image

image

@asichi absolute lifesaver haha ❤️

GrantHair5 avatar Feb 28 '23 11:02 GrantHair5

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.

pauldbentley avatar Mar 02 '23 11:03 pauldbentley

Experiencing this issue too, ty @asichi 👍

rozatoo avatar Mar 24 '23 16:03 rozatoo

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.

ahaleiii avatar May 30 '23 13:05 ahaleiii

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)

hellow554 avatar Oct 06 '23 13:10 hellow554

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

bjornmicallef avatar Oct 29 '23 22:10 bjornmicallef

I got the same issue as @hellow554 .

fangfsz avatar Dec 14 '23 08:12 fangfsz

same . I Excluded it

hwarang098tkd avatar May 02 '24 15:05 hwarang098tkd

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.

giaenuuu avatar Jul 08 '24 11:07 giaenuuu