实体类如下:
namespace Tmp.model
{
[SugarTable(TableName = "Class")]
public class Class
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int ID { get; set; }
public string Name { get; set; }
}
}
//下面通过实体类创建数据库表
db.CodeFirst.InitTables<Class>();
执行完毕后顺利创建数据库表class。下面修改实体类,增加一个字段Remark
namespace Tmp.model
{
[SugarTable(TableName = "Class")]
public class Class
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int ID { get; set; }
public string Name { get; set; }
public string Remark { get; set; }
}
}
//再次通过实体类修改数据库表Class
db.CodeFirst.InitTables<Class>();
运行程序后收入如下错误提示:
SqlSugar.SqlSugarException:“ALTER TABLE 只允许添加满足下述条件的列: 列可以包含 Null 值;或者列具有指定的 DEFAULT 定义;或者要添加的列是标识列或时间戳列;或者,如果前几个条件均未满足,则表必须为空以允许添加此列。不能将列"Remark"添加到非空表"Class"中,因为它不满足上述条件。”
解决方案:给Remark 字段推指定属性值IsNullable=true,修改后的实体类如下:
namespace Tmp.model
{
[SugarTable(TableName = "Class")]
public class Class
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int ID { get; set; }
public string Name { get; set; }
[SugarColumn(IsNullable =true)]
public string Remark { get; set; }
}
}