仅 EF4 代码 +里亚服务
我认为我可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题似乎与实体在删除时处于分离实体状态这一事实有关。
例如,此处描述了类似问题的解决方案。
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.