none
ef 4.1 to 5 issue RRS feed

  • Question

  • I am sure some of you will know the script at the bottom from a very good book programming entity framework.

    I have found the book very helpful in understanding the concepts.

    Unfortunately for me a new-be to EF the book is written for ef 4.1 and I have ef 5 so some code is

    causing me a problem.

    I have manged to work out all the differences so far but i am stuck with this code.

    this line:

    ObjectQuery<DbDataRecord> contacts =

    context.CreateQuery<DbDataRecord>(query);

    throws an error and I found an update for the code:

    ObjectQuery<DbDataRecord> contacts =

    ((IObjectContextAdapter)

    context).ObjectContext.CreateQuery<DbDataRecord>

    (query);

    which worked, but then caused an error with the foreach statement.

    The error has not got an error code and just a message:-

    'The query syntax is not valid. Near identifier 'PEF', line 1, column 27.'

    This is not very helpfull so can anyone show me how to update this code so it will work please?

    Steve

    Full code

    private static void QueryContacts53( ) { using (var context = newPEF( ))

    { var query= "SELECT c, c.Addresses"

    + "FROM PEF.Contact as c"

    + "Where c.FirstName = 'Robert'"; ObjectQuery<DbDataRecord> contacts

    = context.CreateQuery<DbDataRecord>(query); foreach (DbDataRecord c in contacts) { var contact = c[0] asContact; Console.WriteLine("{0}{1}{2}",

    contact.Title.Trim(),

    contact.FirstName.Trim( ),

    contact.LastName.Trim( )); foreach (var a in contact.Addresses) { Console.WriteLine(" {0}, {1}",

    a.Street1,a.City); } } } Console.WriteLine("Press Enter . . . "); Console.ReadLine( ); }



    Friday, May 3, 2013 8:39 AM

Answers

  • It looks some spaces are missing in the query string.

    var query = "SELECT c, c.Addresses " + "FROM PEF.Contact as c " + "Where c.FirstName = 'Robert'";

    Friday, May 3, 2013 8:46 AM
  • Full code

    private static void QueryContacts53( ) { using (var context = newPEF( ))

    { var query= "SELECT c, c.Addresses"

    + "FROM PEF.Contact as c"

    + "Where c.FirstName = 'Robert'"; ObjectQuery<DbDataRecord> contacts

    = context.CreateQuery<DbDataRecord>(query); foreach (DbDataRecord c in contacts) { var contact = c[0] asContact; Console.WriteLine("{0}{1}{2}",

    contact.Title.Trim(),

    contact.FirstName.Trim( ),

    contact.LastName.Trim( )); foreach (var a in contact.Addresses) { Console.WriteLine(" {0}, {1}",

    a.Street1,a.City); } } } Console.WriteLine("Press Enter . . . "); Console.ReadLine( ); }



    I found the issue there is two spaces missing in the query should read:

    var query= "SELECT c, c.Addresses "
    
     		+ "FROM PEF.Contact as c " 
    
    		+ "Where c.FirstName = 'Robert'";

    the spaces come after c.Addresses in the first line and and as c in the second line. so the replacement line now works perfectly.

    Steve

    Friday, May 3, 2013 9:11 AM

All replies

  • It looks some spaces are missing in the query string.

    var query = "SELECT c, c.Addresses " + "FROM PEF.Contact as c " + "Where c.FirstName = 'Robert'";

    Friday, May 3, 2013 8:46 AM
  • Full code

    private static void QueryContacts53( ) { using (var context = newPEF( ))

    { var query= "SELECT c, c.Addresses"

    + "FROM PEF.Contact as c"

    + "Where c.FirstName = 'Robert'"; ObjectQuery<DbDataRecord> contacts

    = context.CreateQuery<DbDataRecord>(query); foreach (DbDataRecord c in contacts) { var contact = c[0] asContact; Console.WriteLine("{0}{1}{2}",

    contact.Title.Trim(),

    contact.FirstName.Trim( ),

    contact.LastName.Trim( )); foreach (var a in contact.Addresses) { Console.WriteLine(" {0}, {1}",

    a.Street1,a.City); } } } Console.WriteLine("Press Enter . . . "); Console.ReadLine( ); }



    I found the issue there is two spaces missing in the query should read:

    var query= "SELECT c, c.Addresses "
    
     		+ "FROM PEF.Contact as c " 
    
    		+ "Where c.FirstName = 'Robert'";

    the spaces come after c.Addresses in the first line and and as c in the second line. so the replacement line now works perfectly.

    Steve

    Friday, May 3, 2013 9:11 AM
  • Umm, yep.
    Friday, May 3, 2013 9:14 AM