Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Code first + Delete Child Object from Parent?
    primarykey
    data
    text
    <p>I have a one-to-many relationship between my table <code>Case</code> and my other table <code>CaseReplies</code>. I'm using EF Code First and now wants to delete a <code>CaseReply</code> from a Case object, however it seems impossible to do such thing because it just tries to remove the CaseId from the specific CaseReply record and not the record itself..</p> <p>I've tried to set cascade delete/update at database level without luck...</p> <p>short: Case just removes the relationship between itself and the CaseReply.. it does not delete the CaseReply.</p> <p>My code:</p> <p>// Case.cs (Case Object)</p> <pre><code>public class Case { [Key] public int Id { get; set; } public string Topic { get; set; } public string Message { get; set; } public DateTime Date { get; set; } public Guid UserId { get; set; } public virtual User User { get; set; } public virtual ICollection&lt;CaseReply&gt; Replies { get; set; } } </code></pre> <p>// CaseReply.cs (CaseReply Object)</p> <pre><code>public class CaseReply { [Key] public int Id { get; set; } public string Message { get; set; } public DateTime Date { get; set; } public int CaseId { get; set; } public Guid UserId { get; set; } public virtual User User { get; set; } public virtual Case Case { get; set; } } </code></pre> <p>// RepositoryBase.cs</p> <pre><code>public class RepositoryBase&lt;T&gt; : IRepository&lt;T&gt; where T : class { public IDbContext Context { get; private set; } public IDbSet&lt;T&gt; ObjectSet { get; private set; } public RepositoryBase(IDbContext context) { Contract.Requires(context != null); Context = context; if (context != null) { ObjectSet = Context.CreateDbSet&lt;T&gt;(); if (ObjectSet == null) { throw new InvalidOperationException(); } } } public IRepository&lt;T&gt; Remove(T entity) { ObjectSet.Remove(entity); return this; } public IRepository&lt;T&gt; SaveChanges() { Context.SaveChanges(); return this; } } </code></pre> <p>// CaseRepository.cs</p> <pre><code>public class CaseRepository : RepositoryBase&lt;Case&gt;, ICaseRepository { public CaseRepository(IDbContext context) : base(context) { Contract.Requires(context != null); } public bool RemoveCaseReplyFromCase(int caseId, int caseReplyId) { Case caseToRemoveReplyFrom = ObjectSet.Include(x =&gt; x.Replies).FirstOrDefault(x =&gt; x.Id == caseId); var delete = caseToRemoveReplyFrom.Replies.FirstOrDefault(x =&gt; x.Id == caseReplyId); caseToRemoveReplyFrom.Replies.Remove(delete); return Context.SaveChanges() &gt;= 1; } } </code></pre> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload