我很努力地删除内存中没有推送到数据库的实体。EF 6代码首先,如何在引用的ICollection中保留删除操作
博客实体在Posts集合中引用0..n个条目。
添加或更改条目公布西港岛线产生更新或插入上下文对象上调用SaveChanges之后的语句。
但是,当我删除一篇文章,然后这个删除操作不推送到数据库。
为了重现我的问题,我使用下面显示的博客/帖子模型创建了这个小样本。
我的方案是一个基于Web的应用程序,其中一个博客条目和所有相关的职位应在第一次请求被加载,然后帖收集应修改,应该被保存到数据库上的其他请求。
我尝试使用下面示例代码中的2个不同的DbContext实例readCtx & writeCtx来模拟此场景。
添加或更改帖子条目工作正常,我得到每个帖子条目的更新语句。
但是:我删除单篇文章的方式不会被推到数据库中,我不知道,EF是如何打算用于此。
如果我删除帖子的条目,如下图所示,然后EF不会忽略它,不会发出delete语句。
的帖子收集应绑定到UI,所以我想里面反映任何更改到数据库中。
我在做什么错?
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
namespace EFDeleteTest
{
public class Blog
{
public Blog()
{
Posts = new List<Post>();
}
public int BlogId { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public virtual Blog Blog { get; set; }
}
public class BlogSystemDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public BlogSystemDbContext() { }
}
class Program
{
private static void LogSql(string sqlStmt)
{
System.Diagnostics.Debug.WriteLine(sqlStmt);
}
static void Main(string[] args)
{
Blog blog = null; // Should be used in the web app for binding it to the UI
using (var readCtx = new BlogSystemDbContext())
{
blog = readCtx.Blogs.Include(b1 => b1.Posts).Where(b2 => b2.Name == "Blog X").SingleOrDefault();
}
// Simulate the next HTTP Request
using (var writeCtx = new BlogSystemDbContext())
{
writeCtx.Database.Log = LogSql;
// delete the last post:
int count = blog.Posts.Count;
var postToDelete = blog.Posts.Skip(count - 1).Take(1).SingleOrDefault();
blog.Posts.Remove((Post)postToDelete);
writeCtx.Blogs.Attach(blog);
writeCtx.Entry(blog).State = blog.BlogId == 0 ? EntityState.Added : EntityState.Modified;
foreach (var post in blog.Posts)
{
writeCtx.Entry(post).State = post.PostId == 0 ? EntityState.Added : EntityState.Modified;
}
writeCtx.SaveChanges();
}
}
}
}
的web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="BlogSystemDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=BlogSystemDB;Integrated Security=true;MultipleActiveResultSets=true " />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
嗨格特,感谢您的回应,这实际上帮助我了解EF如何工作。 我现在知道,所有更改跟踪都是在上下文级别完成的,如果我想要更改跟踪机制来帮助我,我必须附加一个对象。 谢谢 – SteveR 2015-04-11 12:13:52