This is a migrated thread and some comments may be shown as answers.

foreach doesn't work with the new Q2 release

7 Answers 106 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Zsolt
Top achievements
Rank 1
Zsolt asked on 15 Jul 2010, 01:15 PM
Dear All,

 We updated the OpenAccess to version 2010.2.714.1. And we've got some tables in database (MSSQL) and we created a diagram for it. If we writes the following code to iterate over an IQueryable<T> then in the foreach statement the T object will not refreshing:

List<String> listOfRoles = new List<string>();
 
using (EntityDiagrams ctx = new EntityDiagrams())
{
    IQueryable<ROLE> roleList = from roles in ctx.ROLEs
                     select roles;               
 
    foreach (ROLE role in roleList)
    {
        //If the table contains 13 records with different ROLE_NAMEs then the
        //listOfRoles contains the first ROLE_NAME 13 times.
        listOfRoles.Add(role.ROLE_NAME);
    }
}

 What do I wrong? I think that it should be working.

 Regards,
 Zsolt Molnar
 

7 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 15 Jul 2010, 01:38 PM
Hello Zsolt Molnar,

this should work. Can you send us the .rlinq file that you have? Also, please notice that the content of your persistent data is not updated with newest database content when the data was already fetched into the object scope / context.  The easiest way to overcome this is to perform the query in a transaction.Begin()/Rollback() bracket.

As an alternative you might want to consider using a projecting query like:
using (EntityDiagrams ctx = new  EntityDiagrams())
{
    var roleList = from roles in ctx.ROLEs select roles.ROLE_NAME;               
    listOfRoles = new List<string>(roleList);
}

There the return from the LINQ is not a persistence capable instance that could already been fetched, but a string (without identity....).

Kind regards,
Thomas
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Zsolt
Top achievements
Rank 1
answered on 15 Jul 2010, 02:30 PM
Hi Tomas,

 I've tried your code and it was ok! I probed in some way but it works only when the return of the LINQ is not a Telerik persistence class. If the return of the LINQ (or a Lambda Expression) is a persistence class then all name in the list is the same (maybe the first). Only interesting thing is that the count of the iteration is equal with the count of the records in the table what we are selecting.

 The code what I presented here was just a sample code that is similar with our implementation, so I couldn't copy-paste our exact code or share our rlinq file.

 Can we trying to solve this issue from another aspect? What should I do in Visual Studio to repeair the Model if there is something wrong in there? Or what should I check in the rlinq file? (If the rlinq file is the problem how could those iteration work when simple (string) field is selected?)
0
Thomas
Telerik team
answered on 16 Jul 2010, 09:02 AM
Hi Zsolt Molnar,

we thought about this further: When your observed behavior is that the query fails when a PC instances are returned and succeeds when strings are returned, then the difference could be in the in-memory handling.
One reason for this could be that the primary key columns in the database do not match the primary key fields in memory, so that the same object is returned over and over again. Can you verify this please?
We would also really like to know why such a behavior happens, and the surest way to get enough information to us is by sending the .rlinq file (send it through support).

Best wishes,
Thomas
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Zsolt
Top achievements
Rank 1
answered on 16 Jul 2010, 12:25 PM
Hi,

 The database what we have to use doesn't contain primary keys on each tables.This database is not managed by us, and may be updated without our changes. And we don't want to needlessly change these tables everytime when the database is changing.

 Is there any possible solution to use databases with OpenAccess ORM where the primary key is not defined by third party by default? Should we create change scripts for every database updates when it is changing?

 Some of the problem tables does not have any field what we can sign as primary key. I know that this type of databases are poorly designed, but the design is beyond our range. We have only possibility to use it as it is designed. :(

 Just for test I've been change some tables to have primary keys and everything was working fine. Can you suggest what should we do in this situation?

 Kind Regards,
 Zsolt Molnar
0
Alexander
Telerik team
answered on 16 Jul 2010, 03:18 PM
Hello Zsolt Molnar,

Although the tables in the database may not have primary key columns, it is required that each persistent class should have at least one identity field. If there is no primary key in the class, when a query is executed, it would return the first object found in the database repeated as many times as the result count is. This behavior is due to the object cache and if there is no way to identify an object uniquely, the context would always get the same object from the cache.
So the solution would be to manually specify an identity field for each class by selecting the class member and from the Properties pane of Visual Studio change the Identity property to True. It is required though that the values in the column mapped to the property you set as identity are unique. If you do not have such a column you can set several properties (even all of them if necessary) as identity as long as the combination of their values is unique.
Hope that helps.

Best wishes,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Zsolt
Top achievements
Rank 1
answered on 16 Jul 2010, 03:51 PM
Hi,

 Yes, that was what we've tried before I posted my questions.

 I created this Model from the Database. The Visual Studio displays errors about the primary key missing even so I set one or more field's identity to true. And therefore I can't build the project.

 Should I do something else?

 Anyhow, we created a change script to the current version of this database to update table definitions with primary keys. I think this is a temporary solution but haven't got more time to solve this issue. I'm going to holiday tomorrow for 3 weeks, but I'll check your reply from time to time.

 Thanks for your help!

 Regards,
 Zsolt Molnar
0
Alexander
Telerik team
answered on 20 Jul 2010, 03:05 PM
Hi Zsolt Molnar,

Normally marking properties as identity should not cause any build errors. The only case when you may observe such errors is if a nullable property is set as identity. If this is the case, please set the Nullable option for those properties to False or do not use them as identities.
If you still have problems afterwards, please send us the error messages you get so we can help you to solve them.

Regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Zsolt
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Zsolt
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or