开发者

Overriding ToString, Equals,.. in Entities when using Entity Framework

I would like to override ToString, Equals and GetHashCode methods of an Entity generated by the model of the Entity Framework.

What could be the best way of doing this?

开发者_C百科Now I'm manually editing the ___Model.Designer.cs file that generates the model, but everytime I change the model, of course, I lost these changes.


Every Entity is defined as a partial class, so that you can define an additional partial class to extend the Entity with additional properties or methods.

Lets say you have defined an Entity Person in your EntityModel.

Now you can create a new class file name Person.cs in your project. Within the class file you define a

public partial class Person{
   // Here you can add your additional functionality or method overrides
}

The partial class must be defined in the same namespace and the same assembly as the Person Entity.

The new class file is unaffected by changes in the EntityModel (Only if you remove the Entity or change properties, then your partial class might need an update).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜