[英]Store empty string instead of Null in SQL Server database using Entity Framework
There is a third party application table which allows Nulls for a few varchar
columns. 有一个第三方应用程序表,该表允许将Null用于几个varchar
列。 However, if a null is stored on those columns the application crashes. 但是,如果将空值存储在这些列上,则应用程序将崩溃。 I cannot make any changes to either the application or the database. 我无法对应用程序或数据库进行任何更改。
To make matters a little more complicate the models have already been created using a designer and I only have access to a compiled dll that has all the company models. 更复杂的是,已经使用设计器创建了模型,而我只能访问具有公司所有模型的已编译dll。
I have created an ASP.NET MVC page which updates the table however if a column is left blank, a null is inserted in the database. 我创建了一个ASP.NET MVC页面,该页面将更新表,但是如果将列留空,则会在数据库中插入null。
Is there any way to make sure that if a text box is left blank an empty string is stored in the database instead of null? 有什么方法可以确保如果文本框留空,数据库中将存储一个空字符串而不是null?
Add this to the property in your model: 将此添加到模型的属性中:
[DisplayFormat(ConvertEmptyStringToNull = false)]
for example: 例如:
[DisplayFormat(ConvertEmptyStringToNull = false)]
public string ValToBeNotNullInDb {get;set;}
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.convertemptystringtonull(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/system.componentmodel.dataannotations.displayformatattribute.convertemptystringtonull(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.