Omu*_*Omu 6 entity-framework entity-framework-4 entity-framework-4.1
我收到此错误:
System.Data.SqlClient.SqlException DELETE语句与REFERENCE约束"FK_ comments _postId__164452B1" 冲突.冲突发生在数据库"awe",表"dbo.comments",列'postId'中.该语句已终止.
我有这个结构:
public class Post
{
public long Id { get; set; }
public string Body { get; set; }
public long? ParentId { get; set; }
public virtual Post Parent { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public long Id { get; set; }
public long PostId { get; set; }
public virtual Post Post { get; set; }
public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的删除方法:
public void Delete(long id)
{
var p = context.Set<Post>().Get(id);
if(p == null) throw new MyEx("this post doesn't exist");
if (p.Posts.Count > 0) throw new MyEx("this post has children and it cannot be deleted");
context.Set<Post>().Remove(p);
context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
我的DbContext:
public class Db : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9065 次 |
| 最近记录: |