I am accepting 3 fields from a user. All 3 fields are mandatory to be entered. Look at the entity configuration below - all 3 fields in the EntityConfiguration
has been marked as Required
.
This works perfectly when I am Saving a new record to the database. However, now I require to Update an entry. In that case If I ONLY want to update the UserName
, and Password
- there's an exception thrown because Age
can't be Null
. How can I resolved this ?
InnerException = {"Cannot insert the value NULL into column 'Age', table 'DBFFFF.dbo.Users'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
CODE
public class User
{
public int Id{ get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Age { get; set; }
}
public class UserEntityConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("Users");
builder.HasKey(c => c.Id);
builder.Property(c => c.UserName)
.IsRequired(true);
builder.Property(c => c.Password )
.IsRequired(true);
builder.Property(c => c.Age )
.IsRequired(true);
}
}
public async Task<string> UpdateUs(User u)
{
_dbCont.Update(u);
await _dbCont.SaveChangesAsync();
return "ok";
}