Not all FK's updating con savechanges

Liam C. Holmes 1 Reputation point
2021-03-07T17:36:30.957+00:00

Afternoon,
Hope someone can help me, I have the following model:

75151-thisone.png

The rough code (I know its a mess) at present while I am trying to workout what is going on is as follows to add a note and link it to the changelog:

        public async Task CreateNote(NoteViewModel noteViewModel, int customerID, int userID)  
        {  
            var customer = await _unitOfWork.CustomerRepository.GetAsync(customerID);  
  
            customer.CustomerNotes = new Collection<CustomerNote>()  
            {  
                new CustomerNote()  
                {  
                    Note = noteViewModel  
                }  
            };  
  
            await AddChanges(customer, userID, "Create Note");  
  
            _unitOfWork.CustomerRepository.Update(customer);  
  
            await _unitOfWork.Complete();  
        }  
  
        private async Task<Customer> AddChanges(Customer customer, int userID, string changeType)  
        {  
            customer.CustomerChangeLogs.Add(  
                new CustomerChangeLog()  
                {  
                    ChangeLog = new ChangeLog()  
                    {  
                        ChangeType       = changeType,  
                        ChangedByID      = userID,  
                        ChangeLogDetails = await FindChanges(customer)  
                    }                
            });  
            return customer;  
        }  

FindChanges(customer), just iterates through the new details and old details and checks what has been modified, and returns a list and is working as expected.

My issues is that all of the PK/FK's are adding apart from the one with the red arrow at which point I get the following error:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CustomerNote_CustomerChangeLog_CustomerChangeLogID". The conflict occurred in database "Test", table "Logs.CustomerChangeLog", column 'CustomerChangeLogID'. The statement has been terminated.

Not sure how much other code or detail to post up, so if you need anything else please let me know.

Cheers,

Models:

namespace Data.Entities  
{  
    using Data.Entities.Shared;  
    using System.Collections.Generic;  
    using System.Collections.ObjectModel;  
    using System.ComponentModel.DataAnnotations;  
  
    public class Customer  
    {  
        [Key]  
        public int CustomerID { get; set; }  
        [Required]  
        [MaxLength(128)]  
        public string Name { get; set; }  
        public int AddressID { get; set; }  
        public int ContactDetailID { get; set; }  
        public int? ScheduleID { get; set; }  
        public int CompanyID { get; set; }  
  
        public virtual Address Address { get; set; }  
        public virtual ContactDetail ContactDetail { get; set; }  
        public virtual Schedule Schedule { get; set; }  
        public virtual Company Company { get; set; }  
  
        public virtual ICollection<CustomerNote> CustomerNotes { get; set; }  
        public virtual ICollection<CustomerChangeLog> CustomerChangeLogs { get; set; } = new Collection<CustomerChangeLog>();  
        public virtual ICollection<Site> Sites { get; set; }  
    }  
}  
  
namespace Data.Entities  
{  
    using Entities.Shared;  
    using System.ComponentModel.DataAnnotations;  
  
    public class CustomerChangeLog  
    {  
        [Key]  
        public int CustomerChangeLogID { get; set; }  
        public int CustomerID { get; set; }  
        public int ChangeLogID { get; set; }  
  
        public virtual Customer Customer { get; set; }  
        public virtual ChangeLog ChangeLog { get; set; }  
        public virtual CustomerNote CustomerNote { get; set; }  
    }  
}  
  
namespace Data.Entities  
{  
    using Data.Entities.Shared;  
    using System.ComponentModel.DataAnnotations;  
  
    public class CustomerNote  
    {  
        [Key]  
        public int CustomerNoteID { get; set; }  
        public int CustomerID { get; set; }  
        public int NoteID { get; set; }  
        public int CustomerChangeLogID { get; set; }  
  
        public virtual Customer Customer { get; set; }  
        public virtual Note Note { get; set; }  
        public virtual CustomerChangeLog CustomerChangeLog { get; set; }  
    }  
}  
  
  
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,748 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Liam C. Holmes 1 Reputation point
    2021-03-07T20:19:07.71+00:00

    Well I got it, I had to have a copy of the model in both locations when writing to the DB.

    So quick and dirty, i will tidy it up later:

            public async Task CreateNote(NoteViewModel noteViewModel, int customerID, int userID)
            {
                var customer = await _unitOfWork.CustomerRepository.GetAsync(customerID);
    
                customer.CustomerNotes = new Collection<CustomerNote>()
                {
                    new CustomerNote()
                    {
                        Note = noteViewModel,
                    }
                };
    
                await GetChanges(customer, userID, "Create Note");
    
                customer.CustomerNotes.First().CustomerChangeLog = customer.CustomerChangeLogs.First();
    
                _unitOfWork.CustomerRepository.Update(customer);
    
                await _unitOfWork.Complete();
            }
    
    0 comments No comments