C# 错误值转换为null-实体框架
我的数据库中有一个名为C# 错误值转换为null-实体框架,c#,asp.net-core,entity-framework-core,crud,C#,Asp.net Core,Entity Framework Core,Crud,我的数据库中有一个名为HasEvent的布尔列,它的约束为NOTNULL。当我试图通过POST请求将项目添加到表中时,HasEvent为false,它被转换为null,我得到以下错误: Npgsql.postgresception(0x80004005):23502:列“HasEvent”中的null值违反NOTNULL约束 数据显示得恰到好处。会发生什么 PS:当我尝试更新一个已经创建的项目,并通过PUT请求将其HasEvent值设置为false时,我没有得到任何错误 这是我的密码: 实体:
HasEvent
的布尔列,它的约束为NOTNULL。当我试图通过POST请求将项目添加到表中时,HasEvent
为false,它被转换为null,我得到以下错误:
Npgsql.postgresception(0x80004005):23502:列“HasEvent”中的null值违反NOTNULL约束
数据显示得恰到好处。会发生什么
PS:当我尝试更新一个已经创建的项目,并通过PUT请求将其HasEvent
值设置为false时,我没有得到任何错误
这是我的密码:
实体:
public class Period
{
public long Id { get; set; }
public DateTime InitialDate { get; set; }
public DateTime? FinalDate { get; set; }
public bool HasEvent { get; set; }
}
控制器:
[HttpPost]
public ActionResult<Period> Create(Period createPeriod)
{
if (ModelState.IsValid)
{
try
{
var period = Service.Post(createPeriod);
return Created("Created", period);
}
catch (Exception e)
{
return BadRequest(e);
}
}
return BadRequest();
}
编辑:
存储库:
public interface PeriodsRepository<TEntity> : IDisposable where TEntity : class, new()
{
IQueryable<TEntity> SelectAll { get; }
TEntity Select(long key);
void Create(params TEntity[] obj);
void Update(params TEntity[] obj);
void Delete(params TEntity[] obj);
}
public interface PeriodsRepository:IDisposable其中tenty:class,new()
{
IQueryable SelectAll{get;}
触角选择(长键);
无效创建(参数强度[]对象);
无效更新(参数[]obj);
无效删除(参数[]obj);
}
消息说您正在向该列写入null。所以我建议你使用调试器来发现这个问题。嘿,谢谢你的评论。我处理这个问题已经有一段时间了,在它变成“PeriodsRepository.Create(period)”之前,“period”HasEvent值肯定是false,在此之后,我得到了错误。您应该为PeriodsRepository添加代码。您可能还需要为Create方法添加实现详细信息。消息说您正在向该列写入null。所以我建议你使用调试器来发现这个问题。嘿,谢谢你的评论。我处理这个问题已经有一段时间了,在它变成“PeriodsRepository.Create(period)”之前,“period”HasEvent值肯定是false,之后我得到了错误。您应该为PeriodsRepository添加代码。您可能还需要为Create方法添加实现细节。
public interface PeriodsRepository<TEntity> : IDisposable where TEntity : class, new()
{
IQueryable<TEntity> SelectAll { get; }
TEntity Select(long key);
void Create(params TEntity[] obj);
void Update(params TEntity[] obj);
void Delete(params TEntity[] obj);
}