相关疑难解决方法(0)

实体框架4从实体集合中删除对象

我有一个"请求"实体与"RequestProperty"实体的关系为1 ..*.所以在"请求"中有一组RequestProperty对象.当我更新"请求"时,我想删除RequestProperty EntityCollection中的所有项目,并添加来自传入域对象的新项目.当我遍历Request.Properties集合并调用项目中的remove或a DeleteObject时,枚举失败,因为集合已被修改.

截至目前我正在这样做:

while (true)
{
    if (newRequest.Properties.Count > 0)
        context.RequestPropertySet.DeleteObject(newRequest.Properties.First());
    else
        break;
}
Run Code Online (Sandbox Code Playgroud)

由于这不是真的"酷",我认为必须有另一种方法来清空一个关系的集合.谢谢你的想法.

c# entity-framework-4

14
推荐指数
1
解决办法
1万
查看次数

如何清除()实体框架ICollection中的所有元素?

我在使用Clear()从实体框架中的集合中删除所有元素时遇到问题

考虑一下常用的博客和帖子示例.

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; }

    // foreign key to Blog:
    public int BlogId { get; set; } 
    public virtual Blog Blog { get; set; }

    public string Title { get; set; }
    public string Text { get; set; }
}

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework icollection

6
推荐指数
2
解决办法
8336
查看次数