我有这张桌子:
[Table("MyTable", Schema = "MySchema")]
public class MyTable
{
[Key, Column(Order = 0)]
[Required(AllowEmptyStrings = false
public string LanguageID { get; set; }
[Key, Column(Order = 1)]
[Required(AllowEmptyStrings = false
public byte YearID { get; set; }
[Key, Column(Order = 2)]
[Required(AllowEmptyStrings = false)]
public byte SIMSCodeID { get; set; }
[Required(AllowEmptyStrings = true)]
public string Data { get; set; }
[Required(AllowEmptyStrings = true)]
public int UserID { get; set; }
[Required(AllowEmptyStrings = true)]
public int brformula { get; set; }
}
正如您所看到的,最后两个字段是整数。对于那些应该注释的内容,以便在尝试使用AJAX访问此表中的行时不会出现此错误:
消息:
The 'brformula' property on 'MyTable' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'.
异常类型:
System.Data.ConstraintException
发生错误的代码行:
MyTable myTable= db.MyTableDbSet.Find(langid, year, simscodeid);
Api控制器方法:
[HttpGet]
public HttpResponseMessage GetSIMSData(string langid, byte year, byte simscodeid)
{
try
{
MyTable myTable = db.MyTableDbSet.Find(langid, year, simscodeid);
if (MyTable == null)
{
return Request.CreateResponse(HttpStatusCode.OK, this.getSimsDataObject(false, myTable));
}
else
{
return Request.CreateResponse(HttpStatusCode.OK, this.getSimsDataObject(true, myTable));
}
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
我的AJAX电话:
getSIMSDataByAllParams: function (langId, year, simscodeid) {
jq.ajax({
contentType: 'application/json; charset=utf-8',
type: "GET",
url: "/api/MyTable",
data: {
langid: langId,
year: year,
simscodeid: simscodeid,
brformula: 0
},
dataType: "json",
beforeSend: function (jqXHR, settings) {
simsDataView.resetStatusAndErrorTf();
simsDataView.setStatusMsg(simsDataView.getMessageByLang(langId, 'fetchingTreeNodeDataMsg'), constants.TEXT_COLOR_BLUE);
},
success: function (data, textStatus, jqXHR) {
simsDataByAllParams = {};
simsDataByAllParams = JSON.parse(data);
},
complete: function (jqXHR, textStatus) {
if (simsDataByAllParams.hasData) {
tinymce.activeEditor.setContent(simsDataByAllParams.simsDataObject.Data);
} else {
tinymce.activeEditor.setContent(simsDataView.getMessageByLang(langId, 'addEditorContentMsg'));
}
dbDirty = false;
simsDataView.setStatusMsg(simsDataView.getMessageByLang(langId, 'treeNodeDataFetchedMsg'), constants.TEXT_COLOR_GREEN);
},
error: function (jqXHR, textStatus, errorThrown) {
errorsMsgTxt.append(simsUtility.printAjaxError(langId, jqXHR, textStatus, errorThrown));
}
});
},
注意:目前,DB中的字段允许空值,但我希望它的默认值为0。 为什么它不抱怨UserID?它也是整数并允许空值。
答案 0 :(得分:1)
根据我的经验,我可以采用1种或2种方式。
在数据库中设置默认值,或在构造函数中设置默认值。
来自数据库:
如果您正在使用SQL Server(我使用的是什么,但我无法理解为什么此功能在其他DBMS中不可用),您可以单击表中的列design {for brformula
及其下方有一行名为'默认值或Binding'你可以把它设置为0。
来自构造函数:
public class MyTable
{
[Key, Column(Order = 0)]
[Required(AllowEmptyStrings = false
public string LanguageID { get; set; }
[Key, Column(Order = 1)]
[Required(AllowEmptyStrings = false
public byte YearID { get; set; }
[Key, Column(Order = 2)]
[Required(AllowEmptyStrings = false)]
public byte SIMSCodeID { get; set; }
[Required(AllowEmptyStrings = true)]
public string Data { get; set; }
[Required(AllowEmptyStrings = true)]
public int UserID { get; set; }
[Required(AllowEmptyStrings = true)]
public int brformula { get; set; }
public MyTable()
{
brformula = 0;
}
}
我希望这会有所帮助。