I get following exception when selecting from table that has no primary key

Unhandled exception: System.InvalidOperationException: Sequence contains no 
elements
    в System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
    в DbLinq.Data.Linq.DataContext.SetEntitySetsQueries(Object entity) в 
c:\Documents and Settings\e_kotlyarov\Мои 
документы\work\dblinq2007-read-only\src\DbLinq\Data\Linq\DataContext.cs:строка 
521
    в DbLinq.Data.Linq.DataContext._GetOrRegisterEntity(Object entity) в 
c:\Documents and Settings\e_kotlyarov\Мои 
документы\work\dblinq2007-read-only\src\DbLinq\Data\Linq\DataContext.cs:строка 
435
    в DbLinq.Data.Linq.DataContext.Register(Object entity) в c:\Documents 
and Settings\e_kotlyarov\Мои 
документы\work\dblinq2007-read-only\src\DbLinq\Data\Linq\DataContext.cs:строка 
621
    в 
DbLinq.Data.Linq.Sugar.Implementation.QueryRunner.<Select>d__0`1.MoveNext() 
в c:\Documents and Settings\e_kotlyarov\Мои 
документы\work\dblinq2007-read-only\src\DbLinq\Data\Linq\Sugar\Implementation\QueryRunner.cs:строка
 
90
    в Program.Main(String[] args)

Line that causes error is
object primaryKeyValue = (thisPKs.First() as PropertyInfo).GetValue(entity, 
null);

because thisPKs is empty. I made quick fix for it in attach, point of it is 
that it only tries to get primaryKeyValue only if it would be used further 
in procedure.

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

--- DataContext.cs.old	2008-11-19 13:51:15.916225600 +0300
+++ DataContext.cs	2008-11-19 13:45:30.137188600 +0300
@@ -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();
-
-                //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()))
+                foreach (PropertyInfo prop in properties)
                 {
-                    predicate = Expression.Equal(Expression.MakeMemberAccess(p, otherTableMember),
-                                                                Expression.Constant(primaryKeyValue));
+                    //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);
+
+
+                    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 entitySetValue = prop.GetValue(entity, 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"))
                 }
-                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 entitySetValue = prop.GetValue(entity, 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"))
             }
         }
 

Reply via email to