Search code examples
c#entity-frameworkpartial-classesself-modifying

Modifying / Adding to C# Partial Classes


I have got a partial class in my C# project i.e. auto-generated code from Entity Framework. Now I want to modify or add some more functionalities, properties or methods to that class. I have the auto-generated class code under EntityFrameworkModel.tt\Author.cs tab in the project while the other related classes are in another folder i.e. GraphData in the same project.

I also know that the name of partial classes should be same while file name may be different or same as well. I did same but when I defined the object for Author.cs as:

protected override EvoObject ConvertCPV(Author _author)  
{
    if (_author.???)  
    {
        //...  
    }
}

I can't access the methods defined in GraphData\Author.cs (The question marks in example code) whereas the properties defined in EntityFrameworkModel.tt\Author.cs are only accessible.

Here I attached the Solution Explorer image as:

enter image description here

How can I access the properties and methods from both classes?


Solution

  • I have a similar set up in a project also.

    To keep things tidy I also have folders where I place certain partial classes, you just need to ensure the namespace is the same as the auto generated class.

    When you add a new class to a folder the namespace will automatically contain the name of the folder - you can just remove the folder name from the namespace - you should just have the Project name in this scenario.

    If the namespaces are different then the partial classes are not part of the same class. This is why you can't access the new functions/Properties.

    Also, even though your file name and class names can be different, it is better to keep them the same - it will be easier to find a class if the file has the same name.