开发者

Assign empty string to a property with a currently value of nothing

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 avoi开发者_运维百科d 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.


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:

If (<#=code.FieldName(primitiveProperty)#> <> Value) Then

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/ .


The problem is that string comparison in VB treats Nothing and the empty string as equal. You can circumvent this by explicitly using String.Equals.

If Not String.Equals(_UserName, Value) Then …

Better yet, forbid Nothing values completely and initialize the field inside the class to the value of "" (or String.Empty, if you must … which is more or less equivalent).

/EDIT: file a bug report. The generator that generated this code is broken.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜