If no separate Storage is specified, directly set the Column
member, even if it is a writable property instead of a field.

This is not needed for DbMetal-generated files, but should be generally
applicable when writing classes by hand.

Perhaps this should be merged with the following patch, let me known or
apply directly as you prefer.

-- 
Emanuele Aina
Studio Associato Di Nunzio e Di Gregorio
http://dndg.it/
Via Maria Vittoria, 2
10123 Torino - Italy

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DbLinq" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/dblinq?hl=en
-~----------~----~----~----~------~----~------~--~---

>From f7b93e27ff019935caea126f699212d5d77881e7 Mon Sep 17 00:00:00 2001
From: Emanuele Aina <[email protected]>
Date: Fri, 4 Sep 2009 01:27:08 +0200
Subject: [PATCH 5/7] Made the Column Storage handling more robust

If no separate Storage is specified, directly set the Column
member, even if it is a writable property instead of a field.
---
 src/DbLinq/Data/Linq/DataContext.cs |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/DbLinq/Data/Linq/DataContext.cs b/src/DbLinq/Data/Linq/DataContext.cs
index c56791e..0ddc227 100644
--- a/src/DbLinq/Data/Linq/DataContext.cs
+++ b/src/DbLinq/Data/Linq/DataContext.cs
@@ -704,14 +704,27 @@ namespace DbLinq.Data.Linq
 					//it would be interesting surround the above query with a .Take(1) expression for performance.
 				}
 
+				// If no separate Storage is specified, use the member directly
+				MemberInfo storage = memberData.StorageMember;
+				if (storage == null)
+					storage = memberData.Member;
+
+				 // Check that the storage is a field or a writable property
+				if (!(storage is FieldInfo) && !(storage is PropertyInfo && ((PropertyInfo)storage).CanWrite)) {
+					throw new InvalidOperationException(String.Format(
+						"Member {0}.{1} is not a field nor a writable property",
+						storage.DeclaringType, storage.Name));
+				}
+
+				Type storageType = storage.GetMemberType();
 
-				FieldInfo entityRefField = (FieldInfo)memberData.StorageMember;
 				object entityRefValue = null;
 				if (query != null)
-					entityRefValue = Activator.CreateInstance(entityRefField.FieldType, query);
+					entityRefValue = Activator.CreateInstance(storageType, query);
 				else
-					entityRefValue = Activator.CreateInstance(entityRefField.FieldType);
-				entityRefField.SetValue(entity, entityRefValue);
+					entityRefValue = Activator.CreateInstance(storageType);
+
+				storage.SetMemberValue(entity, entityRefValue);
 			}
 		}
 
-- 
1.6.3.3

Reply via email to