Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placement of extended partial classes in entity framework

Since partial classes have to be in the same namespace is my only option to place them in the same directy as my .edmx? If this is the case I am assuming the file name always has to be different.

Also, is there anything additional that I have to do or do I just create another partial class with the same name in the same directory and add properties/methods to it?

like image 617
JTunney Avatar asked May 06 '15 11:05

JTunney


People also ask

Do partial classes have to be in same namespace?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.

Can a partial class implement an interface?

Partial class, interface and structure was introduced in C# 2.0. Now it is possible to split the definition of an class, interface and structure over more than one source files. Moreover the other parts of the class, struct, or interface should be defined in the same namespace or assembly.

How do partial classes work in C#?

A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword.

What is partial class how do you use it and its benefits?

Use of Partial Classes If you are working on a bigger project, splitting the files over different classes helps developers work on the same project simultaneously. If you are working on an automatically generated source, then the code can be added to the class without regenerating the source file.


1 Answers

The partial classes should be in the same project as the .edmx-file. (the same directory is not required).

Say your Entity Model contains an Entity Person. You can create a new file called Person.partial.cs for your other code. You are free to use any filename for your partial classes. It is only necessary that your partial class is defined in the namespace of the Entity.

namespace MyModel{
   public partial class Person {
      // put your additional logic here
   } 
}
like image 121
Jehof Avatar answered Oct 19 '22 01:10

Jehof