skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    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;
    
        //this new line solved the problem
        contact.LinkedUser = null;
    
        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();
    

    Thanks to a few users that commented on the original question and lead me to the solution.


  2. Firstly, you can replace a lot of the checking with:

    if (string.IsNullOrEmpty(contact.FirstName)) 
    {
        contact.FirstName = "FirstName";
    }
    

    Secondly, the error appears to be with the Users relationship, not the Contacts table. I’m assuming that your Contacts table is not called Users in the database, but you might want to check the relationships instead of this specific function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search