【发布时间】:2016-08-17 10:28:32
【问题描述】:
我在迁移中设置了默认值
AlterColumn("Users", "NotNullValue", c => c.String(nullable: false, defaultValue: "Work it!"));
然后我尝试添加新对象,SqlProfiler 显示查询
INSERT [dbo].[WowTable]([NotNullValue],[OnceMoreValue]) VALUES (NULL, 'val')
抛出“无法将 null 值插入列”
当它为空时,有什么方法可以停止插入和更新属性?
更新: 领域类:
public class User
{
public string NotNullValue { get; set; }
public int Id { get; set; }
public string OnceMoreValue { get; set; }
}
更新方法:
public Task AddOrUpdate<TEntity>(TEntity entity) where TEntity : class, IEntity
{
_dbContext.Set<TEntity>().AddOrUpdate(x => x.Id, entity);
return _dbContext.SaveChangesAsync();
}
调用:
AddOrUpdate(new User{OnceMoreValue = "val"});
【问题讨论】:
-
看this。您不能使字符串不可为空。
标签: c# .net entity-framework entity-framework-6