使用绑定源问题删除实体(CTP 5) [英] Removing entities using binding source problem (CTP 5)

查看:49
本文介绍了使用绑定源问题删除实体(CTP 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在通过绑定来源删除项目时遇到问题:


 


  class 程序
{
static void Main( string [] args)
{
DbDatabase.DefaultConnectionFactory = new SqlCeConnectionFactory(" System.Data.SqlServerCe 。4.0" );
DbDatabase.SetInitializer( new DropCreateDatabaseAlways< CategoryContext>());

使用 var categoryContext = new CategoryContext())
{
Add(categoryContext);
}

使用 var categoryContext = new CategoryContext())
{
var bindingSource = new BindingSource();

加载(categoryContext,bindingSource);
Remove(bindingSource,categoryContext);
}

Console.ReadKey();
}

private static void Load(CategoryContext categoryContext,BindingSource bindingSource)
{
bindingSource.DataSource = categoryContext.GetAll< Category>();
Console.WriteLine(" loaded categories count:{0}" ,bindingSource.Count);
//应输出2
}

private static void 删除(BindingSource bindingSource,CategoryContext categoryContext)
{
var categories = categoryContext.GetAll< Category>()。ToList();
bindingSource.Remove(categories [0]);
bindingSource.Remove(categories [1]);
Console.WriteLine("删除后的类别计数:{0}" ,bindingSource.Count);
//应输出0,输出2

Console.WriteLine(" ;类别计数在dbContext中标记为已删除:{0}"
categoryContext.ChangeTracker.Entries()。其中​​(x => x.State == EntityState.Deleted).Count() );
//应输出2

categoryContext.SaveChanges();
}

private static 无效添加(CategoryContext categoryContext)
{
categoryContext.Add( new 类别
{
Name = " category1"
});
categoryContext.Add( new 类别
{
Name = " category2"
});

categoryContext.SaveChanges();
}
}

public class CategoryContext: DbContext
{
public void 添加< T>(T obj)其中 T: class
{
GetObjectSet< T>()。AddObject(obj) ;
}

private ObjectSet< T> GetObjectSet< T>()其中 T: class
{
return ((IObjectContextAdapter) this )。ObjectContext.CreateObjectSet< T>();
}

public IQueryable< T> GetAll< T>()其中 T: class
{
return GetObjectSet< T>();
}

受保护 覆盖 void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Entity< Category>()。Property(x => x.Id)。 HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);
}
}

public class 类别
{
public virtual long Id { get ; set ; }
public virtual string 名称{ get ; set ; }
}

解决方案

Giedrius,


 


使用CTP5时,数据绑定最简单的方法是使用DbSet<> .Local。 
这为您提供了一个ObservableCollection<>显然不包含标记为已删除的实体。 
对于WPF数据绑定,您可以直接绑定到DbSet<> .Local。 
如果需要IBindingList实现,例如WinForms,可以使用.Local上的ToBindingList()扩展方法(在System.Data.Entity命名空间中)创建具有相同行为的IBindingList 。


 


我们希望尽快制作关于数据绑定的博客文章,以提供更多相关文档。


 


谢谢,


Arthur < /跨度>


Hi,

i have a problem removing item through binding source:

 

 class Program
 {
  static void Main(string[] args)
  {
   DbDatabase.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
   DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CategoryContext>());

   using (var categoryContext = new CategoryContext())
   {
    Add(categoryContext);
   }

   using (var categoryContext = new CategoryContext())
   {
    var bindingSource = new BindingSource();

    Load(categoryContext, bindingSource);
    Remove(bindingSource, categoryContext);
   }
   
   Console.ReadKey();
  }

  private static void Load(CategoryContext categoryContext, BindingSource bindingSource)
  {
   bindingSource.DataSource = categoryContext.GetAll<Category>();
   Console.WriteLine("loaded categories count: {0}", bindingSource.Count);
   // should output 2
  }

  private static void Remove(BindingSource bindingSource, CategoryContext categoryContext)
  {
   var categories = categoryContext.GetAll<Category>().ToList();
   bindingSource.Remove(categories[0]);
   bindingSource.Remove(categories[1]);
   Console.WriteLine("categories count after removing: {0}", bindingSource.Count);
   // should output 0, outputs 2

   Console.WriteLine("categories count marked as deleted in dbContext: {0}",
        categoryContext.ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted).Count());
   // should output 2
   
   categoryContext.SaveChanges();
  }

  private static void Add(CategoryContext categoryContext)
  {
   categoryContext.Add(new Category
         {
          Name = "category1"
         });
   categoryContext.Add(new Category
         {
          Name = "category2"
         });

   categoryContext.SaveChanges();
  }
 }

 public class CategoryContext : DbContext
 {
  public void Add<T>(T obj) where T : class
  {
   GetObjectSet<T>().AddObject(obj);
  }

  private ObjectSet<T> GetObjectSet<T>() where T : class
  {
   return ((IObjectContextAdapter)this).ObjectContext.CreateObjectSet<T>();
  }

  public IQueryable<T> GetAll<T>() where T : class
  {
   return GetObjectSet<T>();
  }

  protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
  {
   modelBuilder.Entity<Category>().Property(x => x.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);
  }
 }

 public class Category
 {
  public virtual long Id { get; set; }
  public virtual string Name { get; set; }
 }

解决方案

Giedrius,

 

When using CTP5 the easiest thing for data binding is to use DbSet<>.Local.  This provides you with an ObservableCollection<> that explicitly does not contain entities marked as Deleted.  For WPF data bind you can bind directly to DbSet<>.Local.  If you need an IBindingList implementation, such as for WinForms, you can use the ToBindingList() extension method (in the System.Data.Entity namespace) on .Local to create an IBindingList with the same behavior.

 

We hope to produce a blog post on data binding soon to provide some more documentation on this.

 

Thanks,

Arthur


这篇关于使用绑定源问题删除实体(CTP 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆