I have this code:
foreach (ContactImportRequest row in toImport)
{
Contact contact = _mapper.Map<Contact>(row);
// if first or last names are null, we fill them with anything because are required fields
if (contact.FirstName == null || contact.FirstName.Equals("") || contact.FirstName == String.Empty)
contact.FirstName = "FirstName";
if (contact.LastName == null || contact.LastName.Equals("") || contact.LastName == String.Empty)
contact.LastName = "LastName";
contacts.Add(contact);
}
await _context.Contacts.AddRangeAsync(contacts);
await _context.SaveChangesAsync();
The SaveChangesAsync
call breaks with the following error:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
Npgsql.PostgresException (0x80004005): 23502: null value in column "FirstName" of relation "Users" violates not-null constraint
I’ve debugged and there’s no contact with FirstName
with null value, and just in case I’ve added those two ‘if’ checking if First or Last names are null, setting a fixed value in those cases.
Also, when debugging, there’s no contact in the contacts
list with FirstName
in null.
UPDATE:
The Contacts table has nothing to do with the Users table. they are two separated entities and have different properties.
Here’s the Contact class:
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string? MainPhone { get; set; }
public string? CellPhone { get; set; }
public string? MainEmail { get; set; }
[ForeignKey("LinkedUser")]
public string? LinkedUserId { get; set; }
public virtual User? LinkedUser { get; set; }
}
UPDATE #2:
here’s the previous lines and entire foreach
code:
List<User> users = await _context.Users.AsNoTracking().ToListAsync();
foreach (ContactImportRequest row in toImport)
{
Contact contact = _mapper.Map<Contact>(row);
contact.LinkedUserId = users.FirstOrDefault(x => x.LastName == row.LinkedUserLastName)?.Id;
//if first or last names are null, we fill them with anything because are required fields
if (contact.FirstName == null || contact.FirstName.Equals("") || contact.FirstName == String.Empty)
contact.FirstName = "FirstName";
if (contact.LastName == null || contact.LastName.Equals("") || contact.LastName == String.Empty)
contact.LastName = "LastName";
contacts.Add(contact);
}
await _context.Contacts.AddRangeAsync(contacts);
await _context.SaveChangesAsync();
The only place I deal with Users is to get a list of them, and I’m doing it AsNoTracking.
2
Answers
Auto answering my question with the solution.
The problem was that Entity Framework was considering the
LinkedUser
value as a new User to be created at the moment of Saving changes in the context.This is the final code block working:
Thanks to a few users that commented on the original question and lead me to the solution.
Firstly, you can replace a lot of the checking with:
Secondly, the error appears to be with the
Users
relationship, not theContacts
table. I’m assuming that yourContacts
table is not calledUsers
in the database, but you might want to check the relationships instead of this specific function.