Jeremy Foster
Jeremy Foster

Reputation: 4773

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?

Upvotes: 3

Views: 252

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364399

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;

Upvotes: 5

Related Questions