Afternoon,
Hope someone can help me, I have the following model:
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; }
}
}