Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Issue 249: DbMetal generates wrong code for nullable type properties.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Moritz  
View profile  
 More options Mar 30 2011, 6:39 am
From: Moritz <moritzherrman...@googlemail.com>
Date: Wed, 30 Mar 2011 03:39:24 -0700 (PDT)
Local: Wed, Mar 30 2011 6:39 am
Subject: Issue 249: DbMetal generates wrong code for nullable type properties.
Hello,

I had the same problem descriped in Issue 249. So I created a patch
that is working for me. It's not realy pretty but it's working. Maybe
someone has a better Idea how to fix this or can integrate this in the
production code. I also removed the schema name from the table name. A
thing I always had to do manualy.

Index: CodeDomGenerator.cs
===================================================================
--- CodeDomGenerator.cs (revision 1411)
+++ CodeDomGenerator.cs (working copy)
@@ -639,8 +638,8 @@
                 Name            = table.Type.Name,
                 TypeAttributes  = TypeAttributes.Public,
                 CustomAttributes = {
-                    new CodeAttributeDeclaration("Table",
-                        new CodeAttributeArgument("Name", new
CodePrimitiveExpression(table.Name))),
+                    new CodeAttributeDeclaration("Table", new
CodeAttributeArgument("Name",
+                        new
CodePrimitiveExpression(table.Name.Split('.').Last()))),
                 },
             };

@@ -734,24 +733,21 @@
                 var whenUpdating = new List<CodeStatement>(
                     from assoc in relatedAssociations
                     select (CodeStatement) new
CodeConditionStatement(
-                        new CodePropertyReferenceExpression(
-                            new
CodeVariableReferenceExpression(GetStorageFieldName(assoc)),
-                            "HasLoadedOrAssignedValue"),
-                        new CodeThrowExceptionStatement(
-                            new
CodeObjectCreateExpression(typeof(System.Data.Linq.ForeignKeyReferenceAlrea dyHasValueException)))));
-                whenUpdating.Add(
-                        new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, onChanging, new
CodePropertySetValueReferenceExpression())));
+                        new CodePropertyReferenceExpression(new
CodeVariableReferenceExpression(GetStorageFieldName(assoc)),
"HasLoadedOrAssignedValue"),
+                        new CodeThrowExceptionStatement(new
CodeObjectCreateExpression(typeof(System.Data.Linq.ForeignKeyReferenceAlrea dyHasValueException)))));
+
+                whenUpdating.Add(new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, onChanging, new
CodePropertySetValueReferenceExpression())));
+
                 if (havePrimaryKeys)
-                    whenUpdating.Add(
-                            new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, "SendPropertyChanging")));
-                whenUpdating.Add(
-                        new CodeAssignStatement(fieldReference, new
CodePropertySetValueReferenceExpression()));
+                    whenUpdating.Add(new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, "SendPropertyChanging")));
+
+                whenUpdating.Add(new
CodeAssignStatement(fieldReference, new
CodePropertySetValueReferenceExpression()));
+
                 if (havePrimaryKeys)
-                    whenUpdating.Add(
-                            new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, "SendPropertyChanged", new
CodePrimitiveExpression(property.Name))));
-                whenUpdating.Add(
-                        new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, onChanged)));
+                    whenUpdating.Add(new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, "SendPropertyChanged", new
CodePrimitiveExpression(property.Name))));

+                whenUpdating.Add(new CodeExpressionStatement(new
CodeMethodInvokeExpression(thisReference, onChanged)));
+
                 var fieldType = TypeLoader.Load(column.Type);
                 // This is needed for VB.NET generation;
                 // int/string/etc. can use '<>' for comparison, but
NOT arrays and other reference types.
@@ -760,9 +756,18 @@
                 // Thus, we need to special-case: if fieldType is a
ref or nullable type,
                 //  generate '(field Is value) = false'; otherwise,
                 //  generate '(field <> value)'
-                CodeBinaryOperatorExpression condition =
fieldType.IsClass || fieldType.IsNullable()
-                    ? ValuesAreNotEqual_Ref(new
CodeVariableReferenceExpression(field.Name), new
CodePropertySetValueReferenceExpression())
-                    : ValuesAreNotEqual(new
CodeVariableReferenceExpression(field.Name), new
CodePropertySetValueReferenceExpression());
+                CodeExpression condition = null;
+                if (fieldType.IsClass || fieldType.IsNullable())
+                    condition = ValuesAreNotEqual_Ref(new
CodeVariableReferenceExpression(field.Name),
+                        new
CodePropertySetValueReferenceExpression());
+
+                else if(Provider.FileExtension.Equals("vb") &&
fieldType.IsValueType && column.CanBeNull)
+                    condition = new
CodeSnippetExpression(String.Format("Not Nullable.Equals({0}, value)",
field.Name));
+
+                else
+                    condition = ValuesAreNotEqual(new
CodeVariableReferenceExpression(field.Name),
+                        new
CodePropertySetValueReferenceExpression());
+
                 property.SetStatements.Add(new
CodeConditionStatement(condition, whenUpdating.ToArray()));
                 _class.Members.Add(property);
             }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »