none
Inconsistency in Entity sets RRS feed

  • Question

  • Hi Everyone, 

    I'm using DbContext from Entity Framework 4.1.

    The underlying database is SqlServer Compact Edition 4.0

    I did some left outer join, and then according to the result, I set a property of the object on the left to refer to the object on the right of the join.

    After that, I iterate over the set of objects on the left, which tell me that most of them are referring to an object on the right.

    However if I do a "where" query on the same set, which looks for those referring to a null for that property, it would tell me all of those objects are referring to null.

    I guess it's because when i do a where query, an query is actually carried out on the underlying database table, which was not changed yet since i've not call "SaveChanges" method of the context.

    But this Inconsitency feels really bad. Is that how the Entity Framework supposed to behave ?

     

    The following is part of the code :

     

    using (var db = new LocalDataContext())

                {

                    Console.WriteLine(db.FinancialStatements.Count());        

                    var query = from FinancialStatement fs in db.FinancialStatements

                                join BalanceSheet bs in db.BalanceSheets

                                on new { fs.Ticker, fs.Report_Date } equals new { bs.Ticker, bs.Report_Date }

                                into subBsSet

                                from subBs in subBsSet.DefaultIfEmpty()

                                select new { fs, subBs };

                    Console.WriteLine(query.Count());

     

                    foreach (var row in query)

                    {

                        row.fs.BalanceSheet = row.subBs;

                    }

                    Console.WriteLine("Done");

     

                    Console.WriteLine(query.Where(fs => fs.fs.BalanceSheet == null).Count());

                    Console.WriteLine(db.FinancialStatements.Where(fs=>fs.BalanceSheet != null).Count());

                    Console.Read();

     

                    foreach (var fs in db.FinancialStatements)

                    {

                        Console.WriteLine(fs.BalanceSheet == null);

                    }

                }

     

     

    Friday, November 25, 2011 6:03 AM

Answers

All replies