我在迁移中设置了deafaultValue
AlterColumn("Users", "NotNullValue", c => c.String(nullable: false, defaultValue: "Work it!"));
然后我尝试添加新对象,SqlProfiler显示查询
INSERT [dbo].[WowTable]([NotNullValue],[OnceMoreValue]) VALUES (NULL, 'val')
抛出“无法将值null插入列”
是什么方法在null为空时停止插入和更新属性?
UPD: 域类:
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"});
答案 0 :(得分:0)
您可以使用NotNullValue属性的[Required]属性验证模型:
User
参考:https://msdn.microsoft.com/en-in/data/gg193959.aspx
或者根据我在迁移脚本中看到的内容,您拥有此属性的默认值(“Work It”)。因此,在您的User类构造函数中,您可以设置NotNullvalue =“Work It”,以便在您提到的情况下保存此默认值:
Object
currency_format: "%s%v"
currency_format_decimal_sep: "."
currency_format_num_decimals: "2"
currency_format_symbol: "$"
currency_format_thousand_sep: ","
price_suffix: ""
prices: Object
358: 199.99
__proto__: Object
__defineGetter__: __defineGetter__()
__defineSetter__: __defineSetter__()
__lookupGetter__: __lookupGetter__()
__lookupSetter__: __lookupSetter__()
constructor: Object()
hasOwnProperty: hasOwnProperty()
isPrototypeOf: isPrototypeOf()
propertyIsEnumerable: propertyIsEnumerable()
toLocaleString: toLocaleString()
toString: toString()
valueOf: valueOf()
get __proto__: __proto__()
set __proto__: __proto__()
use_ajax: ""
__proto__: Object
__defineGetter__: __defineGetter__()
__defineSetter__: __defineSetter__()
__lookupGetter__: __lookupGetter__()
__lookupSetter__: __lookupSetter__()
constructor: Object()
hasOwnProperty: hasOwnProperty()
isPrototypeOf: isPrototypeOf()
propertyIsEnumerable: propertyIsEnumerable()
toLocaleString: toLocaleString()
toString: toString()
valueOf: valueOf()
get __proto__: __proto__()
set __proto__: __proto__()