aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Unexpectly delete data in EF Core

Open andreyciocan opened this issue 8 years ago • 1 comments

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 avatar Oct 16 '18 12:10 andreyciocan

This issue was moved to aspnet/EntityFrameworkCore#13645

Eilon avatar Oct 16 '18 16:10 Eilon