简体   繁体   English

使用实体框架在SQL Server数据库中存储空字符串而不是Null

[英]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.

相关问题 实体框架和SQL Server数据库连接字符串 - Entity Framework and SQL Server database connection string 实体框架尝试在数据库中将空字符串另存为null - Entity Framework trying to save empty string as null in database 如何使用实体框架管理在 SQL Server 中存储空字节数组? - How to manage to store null byte array in SQL Server with Entity Framework? 实体框架6导航集合为空而不是空 - Entity Framework 6 navigation collections are null instead of empty 使用.Net MVC3和Entity Framework将文件存储在SQL Server数据库中 - Store file in SQL Server database using .Net MVC3 with Entity Framework 将空字符串转换为 Null 以将 Null 值保存到 SQL Server 数据库中 - Convert empty string into Null to save Null Values into SQL Server database 如何首先使用 Entity Framework 6 代码将 object 作为字符串存储在数据库中 - How to store an object as a string in database using Entity Framework 6 code first 使用实体框架在SQL Server中存储对象列表 - Store list of object in SQL Server using Entity Framework Entity Framework / SQL 服务器:尽管使用 HasConversion,但枚举在数据库中保存为数值而不是字符串<string> ()</string> - Entity Framework / SQL Server: Enums are saving in database as numeric values rather than strings despite using HasConversion<string>() 在SQL Server 2012中使用实体框架(本地* .mdf数据库) - Using Entity framework with SQL Server 2012 (local *.mdf database)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM