English 中文(简体)
先用牛法从收集中删除行刑
原标题:Deleting rows from a collection using ef code first

我有以下领域模式:

public class Campaign
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Content> Content { get; set; }
}

public class Content
{
    public virtual long Id { get; set; }
    public virtual string Body { get; set; }
}

结构如下:

modelBuilder.Entity<Campaign>().HasMany(x => x.Content).WithOptional();

在我任职期间,我有以下法典:

Campaign campaign = campaignRepository.GetById(id);

这把运动和任何相关的内容装入了很庞大的收集工作。 问题涉及以下法典:

 campaign.Name = "new value";
 campaign.Content.Clear();

 unitOfWork.Commit();

这并不删除数据库中的内容。 它实际上将内容表中的外在钥匙确定为受影响行各行无效,但没有删除这些行文。

I then tried to modify the configuration to:

modelBuilder.Entity<Campaign>().HasMany(x => x.Content).WithRequired();

这只会给我以下例外: “社区协会”运动的关系正在删除。 鉴于多重限制,在删除国也必须开展相应的运动——Content_Target。

必须从内容收集中删除行文。 我必须失踪。 感谢任何帮助。

最佳回答

http://msdn.microsoft.com/en-us/library/gg679171%28v=VS.103%29.aspx”rel=“nofollow”

foreach(var content in campaign.Content)
{
    dbContext.Contents.Remove(content);
}
问题回答

暂无回答




相关问题
IObjectSet Include Extension Method Errors with CompiledQuery

In my Custom ObjectContext class I have my entity collections exposed as IObjectSet so they can be unit-tested. I have run into a problem when I use this ObjectContext in a compiled query and call the ...

Problem mapping one to many relationship with EF CTP4

I am trying to define a one to many relationship between Category and Project (a Category can have one or many Projects, a Project can have one or no Category) public class Project : Entity { ...

EF code first - not able to update a database row

Was trying out some code with the EF "code first" method and ran into a strange problem. My datacontext: public class BookmarkerDataContext : DbContext { public DbSet<User> ...

How to set default value for POCO s in EF CF?

In Entity Framework Code First approach, how do you set a default value for a property in the POCO s EntityConfiguration class? public class Person { public int Id { get; set; } public string ...

Map a column to be IDENTITY in db

Although I have marked my ID column with .Identity(), the generated database schema doesn t have IDENTITY set to true, which gives me problems when I m adding records. If I manually edit the database ...

热门标签