EF Entry不会处理已删除的项目吗?

时间:2018-07-13 14:24:16

标签: asp.net-mvc entity-framework crud

我正在使用CRUD从模型(ASP.NET MVC)到数据库进行一些Entity Framework操作。在Action的{​​{1}}内,如何在ControllerViewModel之间映射实体,如何添加/更新实体:

Model

它似乎在新的/更新的记录上很好用。但是被删除的呢?我需要减去列表中的元素,然后手动删除吗?

还是IList<Payments> newPayments = activityViewModel.Payments.Select(p => mapper.Map<PaymentViewModel, Payments>(p)).ToList(); foreach (var payments in newPayments) { ctx.Entry(payments).State = payments.ID == 0 ? EntityState.Added : EntityState.Modified; } 中有一种奇特的方式?

1 个答案:

答案 0 :(得分:1)

如果要删除不在集合中的所有实体,则需要找到要删除的行的键值。这是一个示例:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ef6Test
{
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Id { get; set; }
        public string Content { get; set; }
        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class BlogDbContext : DbContext
    {
        public BlogDbContext() : base("BlogDbContext") { }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }


    class DbInitializer : DropCreateDatabaseAlways<BlogDbContext>
    {
        protected override void Seed(BlogDbContext context)
        {
            base.Seed(context);
            for (int i = 0; i < 100; i++)
            {
                var b = new Blog() { Name = $"Blog {i}" };
                for (int j = 0; j < 10; j++)
                {
                    var p = new Post() { Blog = b, Content = $"Content {i} {j}" };
                    context.Posts.Add(p);
                }
                context.Blogs.Add(b);
                context.SaveChanges();
            }

        }
    }
    class Program
    {


        static void Main(string[] args)
        {
            Database.SetInitializer(new DbInitializer());

            //get an untracked Blog with its Posts
            Blog blog;
            using (var db = new BlogDbContext())
            {
                blog = db.Blogs.Include(b => b.Posts).AsNoTracking().First();

            }

            //remmove some posts
            int i = 0;
            foreach (var p in blog.Posts.ToList())
            {
                i++;
                if (i%2 == 0)
                {
                    blog.Posts.Remove(p);
                }
            }


            //delete the posts that have been removed
            using (var db = new BlogDbContext())
            {
                var postIdsToKeep = blog.Posts.Select(p => p.Id);
                var postIdsToDelete = from p in db.Posts.AsNoTracking()
                                    where p.BlogId == blog.Id
                                    where !postIdsToKeep.Contains(p.Id)
                                    select p.Id;

                //construct key-only entities for the Posts to be deleted
                foreach (var pid in postIdsToDelete)
                {
                    var p = new Post() { Id = pid };
                    db.Entry(p).State = EntityState.Deleted;
                }

                db.Database.Log = m => Console.WriteLine(m);
                db.SaveChanges();
            }
            Console.ReadKey();

        }
    }
}