System.Data.SqlClient.SqlException:无法将值NULL插入AspnetCore Web Api的列中
我正在AspNetCore中创建web api。我想在sql Server中设置默认日期时间值。下面是创建Web Api的方法 控制器:System.Data.SqlClient.SqlException:无法将值NULL插入AspnetCore Web Api的列中,sql,datetime,asp.net-core-webapi,sqlexception,Sql,Datetime,Asp.net Core Webapi,Sqlexception,我正在AspNetCore中创建web api。我想在sql Server中设置默认日期时间值。下面是创建Web Api的方法 控制器: [HttpPost] [Route("Insert/PartyAddress")] public async Task<IActionResult> PostPartyAddressAsync([FromBody] PartyAddress partyAddress) { if
[HttpPost]
[Route("Insert/PartyAddress")]
public async Task<IActionResult> PostPartyAddressAsync([FromBody] PartyAddress partyAddress)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var result = await _repo.PostPartyAddressAsync(partyAddress);
return Ok(result);
}
public async Task<bool> PostPartyAddressAsync(PartyAddress partyAddress)
{
try
{
TblPartyAddress tblPartyAddress = new TblPartyAddress();
tblPartyAddress.PartyId = partyAddress.PartyId;
tblPartyAddress.AddressLine1 = partyAddress.AddressLine1;
tblPartyAddress.AddressLine2 = partyAddress.AddressLine2;
tblPartyAddress.Pincode = partyAddress.Pincode;
tblPartyAddress.City = partyAddress.City;
tblPartyAddress.State = partyAddress.State;
tblPartyAddress.Country = partyAddress.Country;
tblPartyAddress.PersonName = partyAddress.PersonName;
tblPartyAddress.Email = partyAddress.Email;
tblPartyAddress.ContactNo = partyAddress.ContactNo;
tblPartyAddress.IsDefault = partyAddress.IsDefault;
tblPartyAddress.IsActive = partyAddress.IsActive;
await ctx.Context.tblPartyAddresses.AddAsync(tblPartyAddress);
ctx.Context.Entry(tblPartyAddress).State = EntityState.Added;
await ctx.Context.SaveChangesAsync();
return true;
}
catch (Exception ex)
{
return false;
}
}
但是我在向表中插入数据时遇到以下异常
{Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'CreatedDate', table 'AasthaSales.dbo.tblPartyAddress'; column does not allow nulls. INSERT fails.
The statement has been terminated.
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__32.MoveNext()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.<ExecuteAsync>d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__61.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__59.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
{Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时出错。有关详细信息,请参阅内部异常。-->System.Data.SqlClient.SqlException:无法将值NULL插入到“CreatedDate”列、表“AasthaSales.dbo.tblPartyAddress”中;列不允许NULL。插入失败。
声明已终止。
在System.Data.SqlClient.SqlCommand.c.b\u 108\u 0(任务'1结果)
位于System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task和currentTaskSlot)
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.d_u17.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.d_u32.MoveNext()中
---内部异常堆栈跟踪的结束---
在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.d_u32.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.d_u10.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.d_u7`2.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d_u61.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d_u59.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在Microsoft.EntityFrameworkCore.DbContext.d_u48.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
如果有人有疑问,请随时询问
任何帮助都将不胜感激。一个可空的
DateTime
显然会映射到一个允许空的列,因此唯一可能的解释是,在某些情况下,它不是DateTime?
而您将其设置为一个,或者您在它上面有Required
属性并将其删除。在任何情况下,您都需要执行一个新的操作迁移以将这些更改传播回数据库
但是,对于此特定场景,类似于CreatedDate
属性的内容应该是不可为空的,并且只有一个默认设置:
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;
public DateTime CreatedDate { get; set; } = DateTime.UtcNow;