0

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?

maliks
  • 1,102
  • 3
  • 18
  • 42
  • 1
    Including the namespaces where were declared both classes – ocuenca May 16 '16 at 14:44
  • @octavioccl I have added still unable to access – maliks May 16 '16 at 14:46
  • @octavioccl good point, definitely check that to make sure the namespaces match in both files, otherwise it is really two different partial classes you are defining. I would also check to make sure your `GraphData\Author.cs` file is included with the `Build Action` of `Compile`, to make sure it is actually getting compiled into your assembly. – Zack May 16 '16 at 14:48
  • You can simply add another partial class with the same namespace. – ManoDestra May 16 '16 at 16:50

3 Answers3

2

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.

Percy
  • 2,855
  • 2
  • 33
  • 56
  • Namespaces are same but when I remove folder name as: `Namespace.FolderName` when I remove folder name, I got an error that `FolderName` doesn't exist while compiling and also I then be able to access all properties from both – maliks May 16 '16 at 14:59
  • Why can't I add other partial class in separate folder ? – maliks May 16 '16 at 15:00
  • Is the error because you are using the `Namespace.FolderName` in another part of your code somewhere? Now that you have removed the `FolderName` from the Partial Class, you do not need to use `Namespace.FolderName` in the other parts of your code. – Percy May 16 '16 at 15:03
  • @Rick--if I don't use `Namespace.FolderName` in other parts of my code, then still the files and codes changes are saved in the same file/folder ? – maliks May 16 '16 at 15:05
  • Yes - the actual files and code are still in the folder. – Percy May 16 '16 at 15:06
  • Happy to help - this might help you understand a bit more about namespaces [MSDN Namespaces](https://msdn.microsoft.com/en-us/library/zz9ayh33(v=vs.90).aspx) – Percy May 16 '16 at 15:10
0
  • Make your own class outside of EntityFrameworkModel.tt - name it Author.cs, make the class partial.

The whole idea of partial is to allow code generators not to care about your code. Of you modify a generated class, the next regeneration kills the changes.

I did same but when I defined the object for Author.cs as:

Ah, no, you did not because then you claim your definition is:

protected override EvoObject ConvertCPV(Author _author)

Which is NOT defining the object.

I can't access the methods defined in GraphData\Author.cs

Because namespace? Check the namespaces - bad to have them in a subfolder when they belong in a the same namespace as EntityFrameworkModel.tt

whereas the properties defined in EntityFrameworkModel.tt\Author.cs are only accessible.

Partial classes do not allow changes in another partial - not for the moment, a substition syntax is considered.

halfer
  • 19,824
  • 17
  • 99
  • 186
TomTom
  • 61,059
  • 10
  • 88
  • 148
0

Check out https://msdn.microsoft.com/en-us/library/wa80x488.aspx

It says "The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace." It does not explicitly state this, but that implies that the partial definitions of the class must be declared in the same namespace. It doesn't matter which files in the project contain the classes, or what folders they are in.

using System;

namespace DemoConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            SameNamespace.PartialClass.MethodOne();
            SameNamespace.PartialClass.MethodTwo();
            DifferentNamespace.PartialClass.MethodThree();
        }
    }
}

namespace SameNamespace
{
    public partial class PartialClass
    {
        public static void MethodOne()
        {
            Console.WriteLine("Method One.");
        }
    }

    public partial class PartialClass
    {
        public static void MethodTwo()
        {
            Console.WriteLine("Method Two.");
        }
    }
}

namespace DifferentNamespace
{
    public partial class PartialClass
    {
        public static void MethodThree()
        {
            Console.WriteLine("Method Three.");
        }
    }
}
Zack
  • 2,789
  • 33
  • 60