Error field doesn't have a default value in FE Core & MySql

Dung Do Tien Feb 25 2022 48

I have a project web with Asp.Net Core 3.1 and FE Core 3.1, database MySql version 8.

I have created a table name NotifyHistory as below:

Error field doesn't have a default value in FE Core & MySql

NotifyHistoryId column is a primary key and auto increase.

Below is the code I used to insert data for this table:

public async Task<bool> Create(Entities.NotifyHistory model)
{
    if (model == null) return false;
    if (model.UserId <= 0) return false;
    if (!model.PushDate.HasValue) model.PushDate = DateTime.Now;
    if (!model.Status.HasValue) model.Status = false;

    await _db.AddAsync(model);
    await _db.SaveChangesAsync();
    return true;
}

But when I call this method to help insert data, I got an exception Field 'NotifyHistoryId' doesn't have a default value.

 fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'Test.GoogleNotification.Dal.Entities.GoogleNotificationContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
       ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Field 'NotifyHistoryId' doesn't have a default value
         at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
         at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
         at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
         at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
         at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
         at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
         at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
         at System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
      --- End of stack trace from previous location where exception was thrown ---
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

What is that mean, because NotifyHistoryId column is the auto increase? So I no need to set value to it.

How can I solve this error?

Have 2 answer(s) found.
  • S

    Sucharita Ghosh Feb 25 2022

    Sure, the column NotifyHistoryId is auto increase but you still have not set Auto Incremental option to it.

    You have to check the option below:

    Error field doesn't have a default value in FE Core & MySql

    Save it and try again. Your issue will be solved!!

    I hope it helpful to you.

  • S

    Sijesh Thiruthiyil Feb 25 2022

    I think the problem related Strict mode of that table. You can try to run the command below to help disable that mode:

     SET GLOBAL sql_mode = '';

    I hope it works for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close