本文介绍了使用绑定源问题删除实体(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 ; } } 解决方案 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; } } 解决方案 这篇关于使用绑定源问题删除实体(CTP 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 21:26