Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架作为DAL如何正确实现更新和删除_Entity Framework_Data Access Layer - Fatal编程技术网

Entity framework 实体框架作为DAL如何正确实现更新和删除

Entity framework 实体框架作为DAL如何正确实现更新和删除,entity-framework,data-access-layer,Entity Framework,Data Access Layer,我正在用EF4.0编写一个DAL类,我已经读过了 及 但是当我测试他们的代码时,我遇到了更新和删除方法的一些问题 DAL类所有代码如下所示: public class FriendlinkDA : IDisposable { private EdiBlogEntities context; public FriendlinkDA() { context = new EdiBlogEntities(); } public void D

我正在用EF4.0编写一个DAL类,我已经读过了

但是当我测试他们的代码时,我遇到了更新和删除方法的一些问题

DAL类所有代码如下所示:

public class FriendlinkDA : IDisposable
{
    private EdiBlogEntities context;

    public FriendlinkDA()
    {
        context = new EdiBlogEntities();
    }

    public void Dispose()
    {
        context.Dispose();
    }

    public FriendLink GetFriendLink(Guid id)
    {
        return context.FriendLink.FirstOrDefault(f => f.Id == id);
    }

    public void Update(FriendLink model)
    {
        // Way 1:  (throw exception)
        //context.Attach(model);
        //model.SetAllModified(context);
        //context.SaveChanges();

        // Way 2: 
        EntityKey key;
        object originalItem;
        key = context.CreateEntityKey("FriendLink", model);
        if (context.TryGetObjectByKey(key, out originalItem))
        {
            context.ApplyCurrentValues(key.EntitySetName, model);
            //context.ApplyPropertyChanges(key.EntitySetName, model);
        }
        context.SaveChanges();
    }

    public void Delete(FriendLink model)
    {
        // Way 1:
        context.Attach(model);
        context.DeleteObject(model);
        context.SaveChanges();

        // Way 2:
        //var item = context.FriendLink.FirstOrDefault(f => f.Id == model.Id);
        //context.DeleteObject(item);
        //context.SaveChanges();
    }
}
扩展方法是:

public static void SetAllModified<T>(this T entity, ObjectContext context) where T : IEntityWithKey
{
    var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey);
    var propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select
      (pn => pn.FieldType.Name);
    foreach (var propName in propertyNameList)
        stateEntry.SetModifiedProperty(propName);
}
问题1:

在Delete()中,方式1和方式2都可以工作。哪一个更好

问题2:

在Update()中,方式1给了我一个异常:无法附加对象,因为它已经在对象上下文中。对象只有在处于未更改状态时才能重新附着。

在此声明上:上下文。附加(模型)

但是第二条路很好


为什么会这样?我还在Delete()中附加了模型,为什么Delete()工作正常?如何正确编写更新?

例外说明了一切:

对象只有在处于未更改状态时才能重新附着

您可以在
//Update
下更改代码段中的对象,因此无法重新附加该对象

至于哪种方法更好。通常,您会从上下文中获取一个对象,处理该上下文,对该对象执行一些操作,然后使用新上下文保存该对象。在这种情况下,使用
Attach
比首先通过Id获取对象要舒服得多

// Delete
using (var optFriendlink = new FriendlinkDA())
{
    var test = optFriendlink.GetFriendLink(new Guid("81F58198-D396-41DE-A240-FC306C7343E8"));
    optFriendlink.Delete(test);
}

// Update
using (var optFriendlink = new FriendlinkDA())
{
    var testLink = optFriendlink.GetFriendLink(new Guid("62FD0ACF-40C3-4BAD-B438-38BB540A6080"));
    testLink.Title = "ABC";
    optFriendlink.Update(testLink);
}