I am using Dbcontext to provide and ORM for my application, and I defined my entity classes as follow :
public partial class Article
{
public int ArticleID { get; set; }
//other properties
[ConcurrencyCheck]
[Timestamp]
public byte[] TimeStamp { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public partial class Comment
{
public int CommentID { get; set; }
//other properties
public virtual Article Article { get; set; }
}
And for CRUD Operations I follow what you can read in this tutorial
But when I want to delete an article and its associated comments as follow :
var qry=from c in _articleRepo.GetArticleByID(id).Comments
select c;
foreach (var item in qry)
{
_commentRepo.DeleteComment(item.CommentID);
_commentRepo.Save();
}
_articleRepo.DeleteArticle(id);
_articleRepo.Save();
removing comments works good, but when it comes to remove the article, the application throws this exception :
Store update, insert, or delete statement affected an unexpected
number of rows (0). Entities may have been modified or deleted since
entities were loaded. Refresh ObjectStateManager entries.
any one can help me ?
I think you are Saving your context one to many times…
Try removing the _commentRepo.Save(); and just Save on _articleRepo.Save();
Hope this helps…