Hi

Pascal Craponne wrote:
> 
> does this solve all missing PK problems?
> 
It works for me, as far as a understand logic of this code PK is required 
only if there are some foreign keys that reference it, and if there are no 
references there is no reason to try to get value of PK.
Here is patch made against current SVN

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Index: DataContext.cs
===================================================================
--- DataContext.cs	(revision 954)
+++ DataContext.cs	(working copy)
@@ -517,55 +517,58 @@
         private void SetEntitySetsQueries(object entity)
         {
             IList<MemberInfo> properties = DataMapper.GetEntitySetAssociations(entity.GetType());
-            IList<MemberInfo> thisPKs = DataMapper.GetPrimaryKeys(Mapping.GetTable(entity.GetType()));
+            
+            if (properties.Any()) {
+                IList<MemberInfo> thisPKs = DataMapper.GetPrimaryKeys(Mapping.GetTable(entity.GetType()));
 
-            if (thisPKs.Count > 1 && properties.Any())
-                throw new NotSupportedException("Multiple keys object not supported yet.");
+                if (thisPKs.Count > 1)
+                    throw new NotSupportedException("Multiple keys object not supported yet.");
 
-            object primaryKeyValue = (thisPKs.First() as PropertyInfo).GetValue(entity, null);
+                object primaryKeyValue = (thisPKs.First() as PropertyInfo).GetValue(entity, null);
 
 
-            foreach (PropertyInfo prop in properties)
-            {
-                //example of entitySet: Employee.EmployeeTerritories
-                var associationInfo = prop.GetAttribute<AssociationAttribute>();
-                Type otherTableType = prop.PropertyType.GetGenericArguments().First();
+                foreach (PropertyInfo prop in properties)
+                {
+                    //example of entitySet: Employee.EmployeeTerritories
+                    var associationInfo = prop.GetAttribute<AssociationAttribute>();
+                    Type otherTableType = prop.PropertyType.GetGenericArguments().First();
 
-                //other table:EmployeeTerritories
-                var otherTable = GetTable(otherTableType);
-                //other table member:EmployeeTerritories.EmployeeID
-                var otherTableMember = otherTableType.GetProperty(associationInfo.OtherKey);
+                    //other table:EmployeeTerritories
+                    var otherTable = GetTable(otherTableType);
+                    //other table member:EmployeeTerritories.EmployeeID
+                    var otherTableMember = otherTableType.GetProperty(associationInfo.OtherKey);
 
 
-                ParameterExpression p = Expression.Parameter(otherTableType, "other");
-                Expression predicate;
-                if (!(otherTableMember.PropertyType.IsNullable()))
-                {
-                    predicate = Expression.Equal(Expression.MakeMemberAccess(p, otherTableMember),
-                                                                Expression.Constant(primaryKeyValue));
-                }
-                else
-                {
-                    var ValueProperty = otherTableMember.PropertyType.GetProperty("Value");
-                    predicate = Expression.Equal(Expression.MakeMemberAccess(
-                                                                Expression.MakeMemberAccess(p, otherTableMember),
-                                                                ValueProperty),
-                                                             Expression.Constant(primaryKeyValue));
-                }
+                    ParameterExpression p = Expression.Parameter(otherTableType, "other");
+                    Expression predicate;
+                    if (!(otherTableMember.PropertyType.IsNullable()))
+                    {
+                        predicate = Expression.Equal(Expression.MakeMemberAccess(p, otherTableMember),
+                                                                    Expression.Constant(primaryKeyValue));
+                    }
+                    else
+                    {
+                        var ValueProperty = otherTableMember.PropertyType.GetProperty("Value");
+                        predicate = Expression.Equal(Expression.MakeMemberAccess(
+                                                                    Expression.MakeMemberAccess(p, otherTableMember),
+                                                                    ValueProperty),
+                                                                 Expression.Constant(primaryKeyValue));
+                    }
 
-                var query = GetOtherTableQuery(predicate, p, otherTableType, otherTable);
+                    var query = GetOtherTableQuery(predicate, p, otherTableType, otherTable);
 
-                var entitySetValue = prop.GetValue(entity, null);
+                    var entitySetValue = prop.GetValue(entity, null);
 
-                if (entitySetValue == null)
-                {
-                    entitySetValue = Activator.CreateInstance(prop.PropertyType);
-                    prop.SetValue(entity, entitySetValue, null);
+                    if (entitySetValue == null)
+                    {
+                        entitySetValue = Activator.CreateInstance(prop.PropertyType);
+                        prop.SetValue(entity, entitySetValue, null);
+                    }
+
+                    var setSourceMethod = entitySetValue.GetType().GetMethod("SetSource");
+                    setSourceMethod.Invoke(entitySetValue, new[] { query });
+                    //employee.EmployeeTerritories.SetSource(Table[EmployeesTerritories].Where(other=>other.employeeID="WARTH"))
                 }
-
-                var setSourceMethod = entitySetValue.GetType().GetMethod("SetSource");
-                setSourceMethod.Invoke(entitySetValue, new[] { query });
-                //employee.EmployeeTerritories.SetSource(Table[EmployeesTerritories].Where(other=>other.employeeID="WARTH"))
             }
         }
 

Reply via email to