将空字符串分配给当前值为空的属性
在我的 EF4 EntityModel 中,我有一个名为 USERS 的实体。 USERS 具有通用的用户名和密码字符串字段。如果我这样做:
Dim u as new USERS
U.UserName = String.Empty
那么 U.UserName 仍然什么都没有。但如果我这样做:
Dim u as new USERS
u.UserName = “A”
u.UserName = String.Empty
那么 U.UserName 将 String.Empty 作为值,没有问题。
原因是 EF4 生成 UserName 属性的方式
Public Property UserName() As Global.System.String
Get
Return _UserName
End Get
Set
If (_UserName <> Value) Then ‘Here is the key
OnUserNameChanging(value)
ReportPropertyChanging("UserName")
_UserName = StructuralObject.SetValidValue(value, false)
ReportPropertyChanged("UserName")
OnUserNameChanged()
End If
End Set
End Property
我的问题是:
我该如何处理它?
我可以做些什么来避免这种行为吗?我不想每次想将 string.empty 设置为没有任何值的属性时都进行两次分配,并且我不想记住每次都必须以这种方式执行此操作,因为我很确定我会忘记它,然后我会在代码中引入错误。我只想将empty.string值分配给一个属性,并且该属性将“”作为值。
由于我来自 C# 并且是 vb.net 的新手,我真的希望我错过了一些东西。
In my EF4 EntityModel I have an entity named USERS. USERS have the common UserName and Password string fields. If I do something like this:
Dim u as new USERS
U.UserName = String.Empty
Then U.UserName is still nothing. But if I do something like this:
Dim u as new USERS
u.UserName = “A”
u.UserName = String.Empty
then U.UserName takes String.Empty as value without problem.
The reason is the way that EF4 generates the UserName Property
Public Property UserName() As Global.System.String
Get
Return _UserName
End Get
Set
If (_UserName <> Value) Then ‘Here is the key
OnUserNameChanging(value)
ReportPropertyChanging("UserName")
_UserName = StructuralObject.SetValidValue(value, false)
ReportPropertyChanged("UserName")
OnUserNameChanged()
End If
End Set
End Property
My question is:
How can I deal with it?
Is there something I can do to avoid this behavior? I do not want to make two assignations every time I want to set string.empty to a property with a nothing value, and I do not want to remember that I must do it in this way every time, because I'm pretty sure that I will forget it and then I will introduce bugs in the code. I just want to assign the empty.string value to a property and the property take “” as value.
As I come from C# and I'm a newbie in vb.net, I really hope that I’was missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实想分配一个空字符串而不是空值吗?如果不是,您可以将属性设置为可为空吗?
无论哪种方式,您都可以使用 T4 模板(VS 称之为代码生成器)来操作 EF 生成的代码,因此您可以通过这样做将属性设置器实现更改为您想要的任何内容。
编辑 - 添加更多细节
如果您使用 POCO 生成器,您将获得简单的属性,即没有更改属性等。
如果您使用 EntityObject 生成器,它基本上可以让您编辑当前拥有的生成。如果您使用 Visual Studio 的 T4 插件,您可以在 T4 上获得语法突出显示,VS2010 插件中提供了一些非常好的免费插件。您基本上拥有可以在那里生成代码的代码,您可以对其进行调整。您标记的行位于第 523 行:
您可以尝试根据类型做一些有趣的事情,或者您可以恢复使用 Object.Equals() 或您想要的任何其他内容,而不是这一行。或者您可以完全删除 If 部分,但这可能会产生其他后果。当您保存 T4 模板时,它会为您更新所有生成的代码。
根据您实际在做什么,POCO 模板可能会解决您的问题。
顺便说一句,如果您想了解有关 T4 的更多信息,请访问 Oleg Sych 的博客,http://www.olegsych.com/tag /t4/ 。
Do you definitely want to assign an empty string instead of an empty value? If not you could just set the property to be nullable?
Either way you can manipulate the code that EF generates by using T4 templates (VS calls them Code Generators), so you could change the property setter implementation to whatever you wanted by doing that.
Edit - adding a bit more detail
If you use the POCO generator, you get plain properties, i.e. no property changed etc
If you use the EntityObject generator, it basically lets you edit the generation you currently have. If you use a T4 add in for visual studio you get syntax highlighting on the T4, there are some free ones available in the VS2010 add ins which are very good. You basically have code that generates code there, which you can tweak. the line you flagged is on line 523:
You could try and do something funky based on type, or you could just revert to using Object.Equals(), or whatever else you want, instead of this line. Or you could remove the If section entirely but that might have other consequences. When you save the T4 template, it'll update all the generated code for you.
Depending what you are actually doing, the POCO template might cure your problem.
BTW if you want more on T4, visit Oleg Sych's blog, http://www.olegsych.com/tag/t4/ .
问题是 VB 中的字符串比较将Nothing
和空字符串视为相等。您可以通过显式使用String.Equals
来规避此问题。更好的是,完全禁止Nothing
值并将类内的字段初始化为""
(或String.Empty
,如果您必须……这或多或少是等效的)。/编辑:提交错误报告。生成此代码的生成器已损坏。
The problem is that string comparison in VB treatsNothing
and the empty string as equal. You can circumvent this by explicitly usingString.Equals
.Better yet, forbidNothing
values completely and initialize the field inside the class to the value of""
(orString.Empty
, if you must … which is more or less equivalent)./EDIT: file a bug report. The generator that generated this code is broken.