仅 EF4 代码 +里亚服务

发布于 2024-10-03 00:03:32 字数 2070 浏览 3 评论 0原文

我认为我可以将 DbContext 与 DomainService 一起使用,就像与任何 MVC 应用程序一起使用它一样:

public class DatabaseContext : DbContext
{
    public DbSet<User> Users { get; set; }  
}

public class UserDomainService : DomainService
{
    private DatabaseContext db;

    public UserDomainService()
    {
        db = new DatabaseContext();
    }

    public IQueryable<User> GetUsers()
    {
        return db.Users;
    }

    public void UpdateUser(User user)
    {
        db.Users.Attach(user);
    }

    public void DeleteUser(User user)
    {
        db.Users.Remove(user);
    }
}

事实是,当查询工作时,删除和更新操作会抛出异常,例如:

“无法删除该对象,因为它不是在 ObjectStateManager 中找到。”

更新:解决方案

这就是我最终要做的。仍然不确定这是否是正确的方法:

public class DatabaseContext : DbContext
{
    public DbSet<User> Users { get; set; }  
    public new Context ObjectContext { get { return base.ObjectContext; } }
}

public class UserDomainService : DomainService
{
    private DatabaseContext db;

    public UserDomainService()
    {
        db = new DatabaseContext();
    }

    public override bool Submit(ChangeSet changeSet)
    {
        bool submitResult = false;

        try
        {
            submitResult = base.Submit(changeSet);
            db.SaveChanges();
        }
        catch
        {
        }

        return submitResult;
    }

    public IQueryable<User> GetUsers()
    {
        return db.Users;
    }

    public void UpdateUser(User user)
    {
        db.Users.Attach(user);

        var stateEntry = db.Context.ObjectStateManager
                                   .GetObjectStateEntry(entity);

        foreach (var propertyName in stateEntry.CurrentValues
                                          .DataRecordInfo.FieldMetadata
                                          .Select(fm => fm.FieldType.Name))
        {
             stateEntry.SetModifiedProperty(propertyName);
        }
    }

    public void DeleteUser(User user)
    {
        db.Users.Attach(user);
        db.Users.Remove(user);
    }
}

I thought I could use my DbContext with a DomainService just the same way I use it with any MVC app:

public class DatabaseContext : DbContext
{
    public DbSet<User> Users { get; set; }  
}

public class UserDomainService : DomainService
{
    private DatabaseContext db;

    public UserDomainService()
    {
        db = new DatabaseContext();
    }

    public IQueryable<User> GetUsers()
    {
        return db.Users;
    }

    public void UpdateUser(User user)
    {
        db.Users.Attach(user);
    }

    public void DeleteUser(User user)
    {
        db.Users.Remove(user);
    }
}

Thing is that, while the Query works, the Delete and Update operations throw exceptions like:

"The object cannot be deleted because it was not found in the ObjectStateManager."

UPDATED: Solution

So this is what I end up doing. Still not sure if this is the correct way to do it:

public class DatabaseContext : DbContext
{
    public DbSet<User> Users { get; set; }  
    public new Context ObjectContext { get { return base.ObjectContext; } }
}

public class UserDomainService : DomainService
{
    private DatabaseContext db;

    public UserDomainService()
    {
        db = new DatabaseContext();
    }

    public override bool Submit(ChangeSet changeSet)
    {
        bool submitResult = false;

        try
        {
            submitResult = base.Submit(changeSet);
            db.SaveChanges();
        }
        catch
        {
        }

        return submitResult;
    }

    public IQueryable<User> GetUsers()
    {
        return db.Users;
    }

    public void UpdateUser(User user)
    {
        db.Users.Attach(user);

        var stateEntry = db.Context.ObjectStateManager
                                   .GetObjectStateEntry(entity);

        foreach (var propertyName in stateEntry.CurrentValues
                                          .DataRecordInfo.FieldMetadata
                                          .Select(fm => fm.FieldType.Name))
        {
             stateEntry.SetModifiedProperty(propertyName);
        }
    }

    public void DeleteUser(User user)
    {
        db.Users.Attach(user);
        db.Users.Remove(user);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-10-10 00:03:32

该问题似乎与实体在删除时处于分离实体状态这一事实有关。
例如,此处描述了类似问题的解决方案。

The issue seems to be associated with the fact that the entity has Detached entity state at the moment it is deleted.
The solution for a similar problem is described here, for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文