1

Here is my User (Based on ASP Identity):

public class ApplicationUser : IdentityUser
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public string StreetName { get; set; }
    [Required]
    public string StreetNumber { get; set; }
    [Required]
    public string HouseNumber { get; set; }

    public string WeekendShippingStreetName { get; set; }
    public string WeekendShippingStreetNumber { get; set; }
    public string WeekendShippingHouseNumber { get; set; }

    public ICollection<Order> Orders { get; set; }
}

Here is my Order object:

public class Order
{
    public int OrderId { get; set; }

    public string OrderNumber { get; set; }

    public DateTime? FirstDeliveryDate { get; set; }
    public DateTime? LastDeliveryDate { get; set; }

    public DeliveryHours DeliveryHours { get; set; }


    public string Remarks { get; set; }

    public string ApplicationUserId { get; set; }

    public ApplicationUser ApplicationUser { get; set; }

    public InvoiceData InvoiceData { get; set; }

    public bool TermsOfUseAgreement { get; set; }

    public bool Agreement2 { get; set; }

    public DateTime OrderDate { get; set; }

    public Diet Diet { get; set; }

    public decimal TotalAmount { get; set; }

    public bool WantsInvoice { get; set; }

    public bool WantsOtherWeekendAddress { get; set; }

    public bool IsTest { get; set; }

    public bool IsPaid { get; set; }

    [NotMapped]
    public string Password { get; set; }

    public bool IsDeleted { get; set; }
}

And while I am trying to use my repository method:

    public void Delete(string orderNumber)
    {
        try {
            Order order = context.Order.Where(a => a.OrderNumber == orderNumber).FirstOrDefault();

            order.IsDeleted = true;
            order.ApplicationUserId = order.ApplicationUser.Id;

            context.Entry(order).State = System.Data.Entity.EntityState.Modified;

            context.Users.Attach(order.ApplicationUser);
            //context.Order.Add(order);

            context.SaveChanges();
        }
        catch(Exception exception)
        {

        }
    }

I am getting error:

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception.

And inner exception looks like:

InnerException  {
    "The UPDATE statement conflicted with the FOREIGN KEY constraint 
    \"FK_dbo.Orders_dbo.AspNetUsers_ApplicationUserId\". The conflict occurred 
    in database \"aspnet-Dietetyka-20160128071228\", table \"dbo.AspNetUsers\", 
    column 'Id'.\r\nThe statement has been terminated."
}  System.Exception  {System.Data.SqlClient.SqlException}

And I am actually losing my mind, I seems like I have tried everything...

user692942
  • 16,398
  • 7
  • 76
  • 175
  • Im not sure here, but don't you need to mark the Id's with [key]? Or do you have a mapping class? – Martin M Feb 11 '16 at 09:50
  • I have added [Key] To id of my Order, but still without any success... – user3299353 Feb 11 '16 at 09:55
  • Do you need to update the user? Isn't it sufficient to set `order.IsDeleted = true; context.SaveChanges();`? – Douglas Feb 11 '16 at 09:59
  • So I have modified my repository method to look like this: Order order = context.Order.Where(a => a.OrderNumber == orderNumber).FirstOrDefault(); order.IsDeleted = true; order.ApplicationUserId = order.ApplicationUser.Id; context.SaveChanges(); An I have received: exception = {"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."} In inner exception I have found that i have to provide FirstName, LastName and so one... – user3299353 Feb 11 '16 at 10:08
  • `order.ApplicationUserId = order.ApplicationUser.Id` - What happens if you remove that line? The property should already be correctly populated, no? – Douglas Feb 11 '16 at 10:24
  • I am receiving then exception with inner exception like this: InnerException = {"The UPDATE statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Orders_dbo.AspNetUsers_ApplicationUserId\". The conflict occurred in database \"aspnet-Dietetyka-20160128071228\", table \"dbo.AspNetUsers\", column 'Id'.\r\nThe statement has been t... – user3299353 Feb 11 '16 at 11:00
  • So... I have supplied ApplicationUser to my Order and it works well, but it would be still nice to know clean answer... : d – user3299353 Feb 11 '16 at 11:14

0 Answers0