I have a CRUD app deals with two objects, Cards and Logs. When a user creates, edits, or deletes a card, a new Log record is created in the Log table of the database. When the user creates a card, the Log record's NewCard value is JSON data and the OldCard value is null. When the user deletes a card, the Log record's OldCard value is JSON data and the NewCard is null.
However, whenever I delete a card, Entity Framework always creates this extra record circled in red:
Record 2101 is the result after CreateNewLog(card, null)
in IActionResult Create
. Record 2102 and 2103 are the result after CreateNewLog(null, oldCard)
in IActionResult Delete
. Record 2103 is the problem. How do I stop this record from being created?
CardController.cs
private ICardData _cardData;
private ILogData _logData;
public CardController(ICardData cardData, ILogData logData)
{
_cardData = cardData;
_logData = logData;
}
// POST api/card
[HttpPost]
public IActionResult Create([FromBody] Card card)
{
try
{
if (ModelState.IsValid)
{
_cardData.Add(card);
CreateNewLog(card, null);
Response.StatusCode = (int)HttpStatusCode.Created;
return Json(new { Data = card });
}
}
catch (Exception ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Message = ex.Message });
}
return Json("Failed");
}
// DELETE api/card/1
[HttpDelete("{id}")]
public void Delete(int id)
{
Card oldCard = _cardData.Get(id).ShallowCopy();
CreateNewLog(null, oldCard);
_cardData.Delete(id);
}
public void CreateNewLog(Card newCard, Card oldCard)
{
Log newLog = new Log()
{
DateChanged = DateTime.Now,
CardId = newCard == null ? oldCard.CardId : newCard.CardId,
OldCard = JsonConvert.SerializeObject(oldCard),
NewCard = JsonConvert.SerializeObject(newCard),
User = "John Smith"
};
_logData.Add(newLog);
}
LogData.cs
public class SqlLogData : ILogData
{
private LitmusDbContext _context;
public SqlLogData(LitmusDbContext context)
{
_context = context;
}
public IEnumerable<Log> GetAll()
{
return _context.Logs.ToList();
}
public Log Get(int id)
{
return _context.Logs.FirstOrDefault(c => c.Id == id);
}
public void Add(Log newLog)
{
_context.Add(newLog);
_context.SaveChanges();
}
}
CardData.cs
public class SqlCardData : ICardData
{
private LitmusDbContext _context;
public SqlCardData(LitmusDbContext context)
{
_context = context;
}
public IEnumerable<Card> GetAll()
{
return _context.Cards.ToList();
}
public Card Get(int id)
{
Card card = _context.Cards.FirstOrDefault(c => c.Id == id);
return card;
}
public void Add(Card newCard)
{
_context.Add(newCard);
_context.SaveChanges();
}
public int Commit()
{
_context.SaveChanges();
return 0;
}
public void Update(Card newCard)
{
var oldCard = Get(newCard.Id);
if (oldCard != null)
{
oldCard.CardId = newCard.CardId;
oldCard.State = newCard.State;
_context.SaveChanges();
}
}
public void Delete(int id)
{
var cardToDelete = Get(id);
_context.Remove(cardToDelete);
_context.SaveChanges();
}
}