首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架6:删除子框架

实体框架6:删除子框架
EN

Stack Overflow用户
提问于 2014-07-30 20:36:36
回答 1查看 2K关注 0票数 1

当我想删除子项目时,我遇到了一个奇怪的问题:

我的模型:

代码语言:javascript
复制
public class Blog
{
    public Blog() {}

    [Key]
    [Column(Order=0)]
    public int Id { get; set; }
    public string name { get; set; }
    public BlogDetail BlogDetail { get; set; }
    public List<Post> Posts { get; set; }
}

public class BlogDetail
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Name {get ; set;}
    public int BlogId { get; set; }
}

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer<BlogContext>(new BlogInitializer());

        using (var context = new BlogContext())
        {
            context.Database.Initialize(true);

            context.Blogs.Add(new Blog()
            {
                name = "My First blog",
                BlogDetail = new BlogDetail() { Name = "Test" },

                Posts = new List<Post> { new Post { Name = "Post1" }, new Post { Name = "Post2" } }

            });

            context.Blogs.Add(new Blog
            {
                name = "Test3"
            });

            context.SaveChanges();

            Blog blog = context.Blogs.Include(f => f.BlogDetail)
                .Where(d => d.name.Equals("Mon premier blog")).SingleOrDefault();

            ((BlogDetail)blog.BlogDetail).Name = "Test1";

            context.SaveChanges();

            // try to delete the blog.
            context.Entry(blog.BlogDetail).State = EntityState.Deleted;

            // Doesn't work : throw : 
            // Additional information: Collection was modified; enumeration operation may not execute.
            blog.Posts.ForEach(c => context.Entry(c).State = EntityState.Deleted);

            // Doesn't work : throw 
            //The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 
            //When a change is made to a relationship, the related foreign-key property is set to a null value. 
            //If the foreign-key does not support null values, a new relationship must be defined, the 
            //foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
            context.Entry(blog).State = EntityState.Deleted;
            context.SaveChanges();

    }
}

我找不到一个干净的解决方案来删除我的帖子!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-30 21:24:11

试一试

代码语言:javascript
复制
context.BlogDetail.Remove(blog.BlogDetail)
context.SaveChanges()

所以我认为你应该删除子对象

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25046457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档