Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Does Entity Framework Code First Change my Type Names?

I generated a bunch of classes using EF 4.1 Power Toys to reverse engineer my database. My classes and maps look good and work well, but when I check the name of a type that is returned, it appears that EF has added a GUID to the type name. So a method with this for the body:

var context = new DbContext();
var myVehicle = context.Vehicles.First();
return myVehicle.GetType().Name;

...would return something like:

"Vehicle_F31E8AC6EB21A3220F761E7C2FFEB3B433CEFC7CF567E9A0CF53E8F4B598C2B9"

Why is this and is there any way to turn it off?

like image 809
Jeremy Foster Avatar asked Nov 27 '25 09:11

Jeremy Foster


1 Answers

It is called dynamic proxy. When you query the type from entity framework for the first time it will dynamically create class derived from your entity type and return it instead. The name you see is the name of that derived class.

The reason why entity framework does this is to support some advanced features like lazy loading or dynamic change tracking. It can be turned off by calling:

context.Configuration.ProxyCreationEnabled = false;
like image 110
Ladislav Mrnka Avatar answered Nov 29 '25 00:11

Ladislav Mrnka