Unexpectly delete in EF Core
I have a huge issue with entity framework in Microsoft.AspNetCore.App v 2.1.2.
I have the following two models:
public class IPJPeriod
{
[Key]
public int IPJPeriodId { get; set; }
public int UserId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int ImproperDays { get; set; }
public int ProperDays { get; set; }
public string Details { get; set; }
public bool IsDeleted { get; set; }
}
and
public class AddedSubtractedPeriod
{
[Key]
public int AddedSubtractedPeriodId { get; set; }
public bool IsAdded { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public System.Int16 AddedSubtractedYears { get; set; }
public System.Byte AddedSubtractedMonths { get; set; }
public System.Int16 AddedSubtractedDays { get; set; }
[StringLength(10)]
public string DecisionNo { get; set; }
public DateTime? DecisionDate { get; set; }
public int? InstanceId { get; set; }
public int AddedSubtractedPeriodReasonId { get; set; }
public DateTime RecordingDate { get; set; }
public int UserId {get; set;}
}
In my code I get the list of IPJPeriod then use a foreach to iterate the records. On some math calculations I add or not a AddedSubtractedPeriod into the database. The problem is that when I call await _context.SaveChangesAsync() it deletes my IPJPeriod's without calling any Remove function.
This is my Controller code snippet:
var IPJPeriods = _context.IPJPeriod
.Where(x => x.UserId == id && x.IsDeleted == false)
.ToList();
if (IPJPeriods != null && IPJPeriods > 0)
{
foreach (var item2 in PrisonerIPJPeriods)
{
if ((item2.ImproperDays / 30) * 6 > 0)
{
var AddedSubtractedPeriod = new PrisonerFinalConvictionAddedSubtractedPeriod
{
IsAdded = false,
StartDate = item2.StartDate,
EndDate = item2.EndDate,
AddedSubtractedYears = 0,
AddedSubtractedMonths = 0,
AddedSubtractedDays = (Int16)((item2.ImproperDays / 30) * 6),
AddedSubtractedPeriodReasonId = 11,
RecordingDate = DateTime.Now,
UserId = id
};
_prisonerContext.PrisonerFinalConvictionAddedSubtractedPeriod.Add(AddedSubtractedPeriod);
await _prisonerContext.SaveChangesAsync(); //huge issue. Before foreach I had for example 4 records in IPJPeriods, but after this call I have none...
[ more code ....]
}
@andreyciocan We have not been able to reproduce this. Can you please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing.
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.