【问题标题】:Entity Framework as DAL how to implement Update and Delete correctly实体框架作为 DAL 如何正确实现更新和删除
【发布时间】:2012-08-16 03:38:59
【问题描述】:

我正在使用 EF4.0 编写 DAL 类,我已阅读

http://www.codeproject.com/Articles/43367/ADO-NET-Entity-Framework-as-Data-Access-Layer

http://msdn.microsoft.com/en-us/magazine/cc700340.aspx

但是当我测试他们的代码时,我遇到了 Update 和 Delete 方法的一些问题。

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);
}

在应用程序中,我像这样使用 DAL:

// 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);
}

问题一:

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

问题 2:

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

关于此声明:context.Attach(model);

但是方式 2 很好。

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

【问题讨论】:

    标签: entity-framework data-access-layer


    【解决方案1】:

    例外说明了一切:

    一个对象只有在它处于未更改状态时才能重新附加。

    你在// Update下的代码sn-p中更改了对象,所以无法重新附加。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 2013-12-03
      • 1970-01-01
      • 2016-06-22
      • 2016-06-09
      • 1970-01-01
      • 2016-11-17
      相关资源
      最近更新 更多