简体   繁体   English

为什么实体框架代码首先更改我的类型名称?

[英]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. 我使用EF 4.1 Power Toys生成了一堆类来对我的数据库进行逆向工程。 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. 我的类和地图看起来很好并且运行良好,但是当我检查返回的类型的名称时,似乎EF已经为类型名称添加了GUID。 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? 为什么会这样,有什么办法可以关掉它?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM