请教个关于Insert的问题 返回
SqlSugar
老数据
5
4650
悬赏:0 飞吻
MSSQL数据库,表里有字段 Id,FullName,IsEnabled,CreateDate,其中IsEnabled在数据库里设有默认值0,CreateDate默认值为getdate(),
在C#代码中
using (var db = SugarDAO.GetInstance()) { var model = new BaseModule(); model.Id = Guid.NewGuid().ToString(); model.FullName = "基本资料"; var r= db.Insert(model); MessageBox.Show(r.ToString()); }
写入后的数据里IsEnabled,CreateDate两个字段都是NULL,请问是什么原因导致默认值不会自动生成?或者是默认值都要在model里赋值?
热忱回答(5)
-
fate stay night VIP0
2016/12/7using (var db = SugarDAO.GetInstance()) { var model = new BaseModule(); model.Id = Guid.NewGuid().ToString(); model.FullName = "基本资料"; db.AddDisableInsertColumns("CreateDate");//禁止插入CreateDate var r= db.Insert(model); MessageBox.Show(r.ToString()); }禁止插入后就可以走默认值了
0 回复 -
fate stay night VIP0
2016/12/7db.AddDisableInsertColumns("CreateDate"."IsEnabled");或者写在实体构造函数赋值public class BaseModule{
public BaseModule(){
createDate=datetime.now;
}
public datetime createDate {get;set;}
}
0 回复 -
扬帆起航 VIP0
2016/12/10谢谢,请问2楼回贴的代码怎么插入?在编辑器里都找不到。
0 回复 -
扬帆起航 VIP0
2016/12/10@fate stay night:insert默认返回Identity,如果没有Identity执行成功将返回true,那就是判断返回null或false就是失败?或者有什么更好的方法吗?
0 回复 -
fate stay night VIP0
2016/12/10@扬帆起航:return Convet.ToBool(Insert(obj)) 如果没有Identity
0 回复