是否有一个选项可以使实体框架将空字符串恢复为 null?
我正在使用 ADO.NET Entity-Framework ObjectContext 来访问我的数据存储。
我希望如果值设置为空字符串,它们应该自动变为空。
I am using an ADO.NET Entity-Framework ObjectContext to access my data store.
I want the if values are set with empty strings, they should automatically become null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用 Entity Framework 4,则可以使用 T4 模板来完成此操作。 只需将其放在 .tt 模板文件中每个字符串属性的 getter 中,它就会用 null 替换空字符串并自动修剪字符串。 无需使用反射。
If your using Entity Framework 4, you can use T4 templates to accomplish this. Just place this in the getter of each string property in your .tt template file, and it will replace empty strings with null and automatically trim strings. No need to use reflection.
我实际上找到了一种更好的方法,它实际上是内置在系统中的,而且它使用无论如何加载的实体的内部序数元数据(我还没有测试性能差异,但这应该比反射):
I actually found a better way to this, it's actually built in the system, plus it uses the internal ordinal metadata of the entities which are loaded anyway (I haven't tested the performance difference, but this should be hell of a lot faster than reflection):
我刚刚将上面的代码改编为新版本的 Entity Framework 4.1 (DbContext)。
I've just adapted the code above to the new release of Entity Framework 4.1 (DbContext).
据我所知。
您可以编写一个继承自
ObjectContext
的类并重写SaveChanges()
来执行此操作,并在 x.objectlayer 中使用它而不是ObjectContext
.cs/x.designer.csNot that I'm aware of.
You could possibly write a class that inherited from
ObjectContext
and overrideSaveChanges()
to do that and use that instead ofObjectContext
in your x.objectlayer.cs / x.designer.cs我没有通过覆盖 ObjectContext 让事情变得复杂,而是使用字符串扩展方法来转换数据库存储的值
Rather than complicating things by overriding the ObjectContext I use string extension methods to convert values for database storage
这是 Entity Framework Core 的解决方案(在 V2 中测试)。 由于文档有限,我不得不通过 API 进行尝试,因此可能还有其他方法可以完成同样的事情。 请注意,使用此方法修改了原始对象。
Here is a solution for Entity Framework Core (tested in V2). Had to bang my way through the API due to limited documentation so there might be other ways to accomplish the same thing. Note that the original objects are modified using this method.
为了完整起见,这里是将接受的答案编写为部分类而不是继承类。 请注意,此版本还会修剪字符串。
我还展示了所需的使用,因为当我复制粘贴代码并且 IDE 抛出一些有关未找到类型或命名空间的错误时,我发现这令人沮丧。
Just for completeness, here is the accepted answer written as a partial class instead of inherited. Note that this version also trims strings.
I have also shown the required
usings
because I find it frustrating when I copy'n'paste code and the IDE throws up some errors about type or namespace not found.我使用了 Shimmy 的解决方案并且很高兴,直到我发现我的复杂类型中的字符串被遗漏了。 换句话说,我需要一种方法来不仅在我的核心对象/记录中,而且在它的任何非原始对象属性、他们的属性和他们的属性中清空可空的空白字符串......
以下是我的递归改编。 我无法谈论它的优雅或生产质量,因为它上线的时间不长,但到目前为止它似乎对我有用,并且至少可以作为其他人的起点。
I used Shimmy's solution and was pleased until I discovered that strings in my complex types were being missed. In other words, I needed a way to null out nullable whitespace-only strings in not only my core object/record, but any of its non-primitive object properties, and theirs, and theirs...
Following is my recursive adaptation. I can't speak to its elegance or production quality because it hasn't been live for very long, but it appears to be working for me so far and may at least serve as a starting point for someone else.