我有一个使用绑定源和数据网格的c#winforms应用程序。
我正在使用Julie Lerman的存储库模式来设置绑定到网格的数据。
patern依赖于实现IEntity接口的实体
public interface IEntity
{
EntityState State { get; set; }
}
当我编辑记录并尝试保存时,我的更改不会被保存,这是因为对绑定实体的更改不会设置IEntity.State属性。
我应该怎么做才能设置State属性?
设置bindingSource的代码是
using (var repo = new PeopleRepository(new OrganisationContext()))
{
IList<Person> lst = repo.GetList(p => p.Id > 0 && p.Archived == false, x => x.Organisation);
bindingSource.datasource = (List<Person>)lst;
}
存储库代码包含
public virtual IList<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
{
List<T> list;
IQueryable<T> dbQuery = ((DbContext)this.context).Set<T>();
//Apply eager loading
foreach (var navigationProperty in navigationProperties)
{
dbQuery = dbQuery.Include(navigationProperty);
}
list = dbQuery.AsNoTracking().Where(where).ToList();
return list;
}
保存时我调用以下内容,但由于未设置状态标志,因此无效。
public void Save(IEnumerable<T> entities)
{
if (entities.Count(p => ((IEntity)p).State == EntityState.Added) > 0)
{
this.Add(entities.Where(p => ((IEntity)p).State == EntityState.Added).ToArray());
}
if (entities.Count(p => ((IEntity)p).State == EntityState.Modified) > 0)
{
this.Update(entities.Where(p => ((IEntity)p).State == EntityState.Modified).ToArray());
}
if (entities.Count(p => ((IEntity)p).State == EntityState.Deleted) > 0)
{
this.Remove(entities.Where(p => ((IEntity)p).State == EntityState.Deleted).ToArray());
}
}
[更新] 我正在使用绑定导航器添加和删除按钮