简体   繁体   English

实体框架中的合并

[英]MERGE in Entity Framework

Is there a way to call T-Sql's MERGE command from .NET Entity framework 4?有没有办法从 .NET Entity framework 4 调用T-Sql 的 MERGE命令?

No there no such built-in functionality - you must build your own.不,没有这样的内置功能 - 您必须自己构建。 Very common is for example approach like:非常常见的方法例如:

public void SaveOrUpdate(MyEntity entity)
{
    if (entity.Id == 0)
    {
        context.MyEntities.AddObject(entity);
    }
    else
    {
        context.MyEntities.Attach(entity);
        context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
    }

    // You can call SaveChanges here or you can call it separately after multiple changes
}

This is example for working with detached entity which have Id auto generated in the database ( IDENTITY ).这是使用已在数据库中自动生成的Id ( IDENTITY ) 的分离实体的示例。 Default Id for new entity is always 0 because the real value will be assigned during saving changes.新实体的默认 ID 始终为 0,因为实际值将在保存更改期间分配。

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

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