简体   繁体   English

创造 <Object> vs新 <Object> 在实体框架中

[英]Create<Object> vs new <Object> in Entity Framework

I've got an initializer/updater for an entity object. 我有一个实体对象的初始化程序/更新程序。 Is there any danger in using 使用中是否有任何危险

Dim myObject As SpecialThing = New SpecialThing()

Then setting all the values (using the updater that is already written), or do I need to use: 然后设置所有值(使用已写入的更新程序),或者我是否需要使用:

Dim myObject As SpecialThing = SpecialThing.Create()

There are 30 parameters and the updater already sets the values/handles errors. 有30个参数,updater已设置值/处理错误。 Just looking to reuse that code. 只是想重用那段代码。

I don't know what exactly you mean with myDB.CreateSpecialThing(.....) . 我不知道你对myDB.CreateSpecialThing(.....)究竟是什么意思。 I have three interpretations: 我有三种解释:

  • objectContext.CreateObject<SpecialThing>()

  • dbContext.SpecialThings.Create() (EF >= 4.1) dbContext.SpecialThings.Create() (EF> = 4.1)

  • SpecialThing.Create(.....) (a static method of EntityObject derived entities) SpecialThing.Create(.....)EntityObject派生实体的静态方法)

The third method is only an autogenerated helper which takes parameters (for the required fields), sets properties and returns the object. 第三种方法只是一个自动生成的帮助器,它接受参数(对于必需的字段),设置属性并返回对象。 It's exactly the same as creating the object with new and setting properties afterwards. 这与之后使用new和setting属性创建对象完全相同。

The first two methods come into play if you are working with POCOs and use lazy loading or change tracking proxies. 如果您正在使用POCO并使用延迟加载或更改跟踪代理,前两种方法将发挥作用。 These methods will create a dynamic proxy of the entity (which is a dynamic class derived from your entity class) and not directly the entity. 这些方法将创建实体的动态代理(它是从您的实体类派生的动态类),而不是直接创建实体。 None of these methods attach the entity to the context, you must do this manually - no matter if you use these methods to create the entity or create it with new . 这些方法都不会将实体附加到上下文中,您必须手动执行此操作 - 无论您是使用这些方法创建实体还是使用new创建实体。

Example where using CreateObject<T> / Create can be important, assuming a User entity with a virtual Roles collection: 假设具有virtual Roles集合的User实体,使用CreateObject<T> / Create示例可能很重要:

using (var ctx = new MyDbContext())
{
    var user = ctx.Users.Create();
    user.Id = 1;
    ctx.Users.Attach(user);

    var roles = user.Roles;
}

Using virtual enables lazy loading for the Roles collection and the code above would load all roles of user 1 (or an empty collection if the user has no roles). 使用virtualRoles集合启用延迟加载,上面的代码将加载用户1的所有角色(如果用户没有角色,则加载空集合)。 Using new on the other hand... 另一方面使用new ...

using (var ctx = new MyDbContext())
{
    var user = new User { Id = 1 };
    ctx.Users.Attach(user);

    var roles = user.Roles;
}

...doesn't allow to lazily load the collection because user is not a dynamic proxy object. ...不允许懒惰加载集合,因为user不是动态代理对象。 roles would be null , no matter if the user has roles or not. 无论用户是否具有角色, roles都将为null

So, I'd say that there is no danger to create an entity with new . 所以,我要说创建一个具有new的实体是没有危险的。 You just have to keep in mind that you don't have the features of lazy loading or change tracking proxies for an entity created with new . 您只需要记住,您没有使用new创建的实体的延迟加载或更改跟踪代理的功能。

If you create the object yourself, it won't be attached to the context. 如果您自己创建对象,则不会将其附加到上下文。 You'll need to attach the object in order to have changes update in the database. 您需要附加对象才能在数据库中更新更新。

Eventhough if you create Entity using Create Method, it will not be attached to the context and this will save in DB by SaveChanges method. 尽管如果使用Create Method创建实体,它将不会附加到上下文,这将通过SaveChanges方法保存在DB中。 http://msdn.microsoft.com/en-us/library/gg696136(v=vs.113).aspx http://msdn.microsoft.com/en-us/library/gg696136(v=vs.113).aspx

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

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