2

Trying to convert to EF 5 but having problems with CreateQuery. Any help would be much appreciated. Thank you in advance.

This is the code:

using System;
using System.ServiceModel;
using System.Data.Objects;
namespace TestingOneServiceRole
{
    public class Service1 : IService1
{
public void AddUser(string fullName, string userName, string password)
    {
        using (var context = new MyEntities())
        {
            context.AddToUser(new User()
            {
                UserName = userName,
                Password = password,
                FullName = fullName,
            });
            context.SaveChanges();
        }
    }
    public string UserLoginNow(string username, string password)
    {
        string query = @"SELECT value blah.FullName FROM MyEntities.blah AS User WHERE blah.UserName = @username AND blah.Password = @password";
        ObjectParameter[] parameters = new ObjectParameter[2];
        parameters[0] = new ObjectParameter("username", username);
        parameters[1] = new ObjectParameter("password", password);
        using (var context = MyEntities())
        {
            ObjectQuery<string> results = context.CreateQuery<string>(query, parameters);
            foreach (string result in results)
            {
 etc.......
2
  • Whats problems are you having? Commented Apr 26, 2013 at 15:56
  • I get an error that "TestingOneServiceRole.MyEntities" does not contain a definition for "CreateQuery" and no extension method... Commented Apr 26, 2013 at 16:05

2 Answers 2

2

I assume in this response that your MyEntities inherits from DbContext in EF5.

Use the SqlQuery(of T) method in the DbContext.Database property to accomplish the same thing:

public string UserLoginNow(string username, string password)
{
    string query = @"SELECT value blah.FullName FROM MyEntities.blah AS User WHERE blah.UserName = @username AND blah.Password = @password";
    object[] parameters = new object[2];
    parameters[0] = username;
    parameters[1] = password;
    using (var context = MyEntities())
    {
        IEnumerable<string> results = context.Database.SqlQuery<string>(query, parameters);
        foreach (string result in results)
        {
        }
    }
}

If your entity framework model dervies from ObjectContext (as opposed to DbContext) then see this link provided by @MarkSowul on how to get access to the CreateQuery method in EF 5.

Sign up to request clarification or add additional context in comments.

4 Comments

This is not composable. One workaround mentioned in the forums is to cast to IObjectContextAdapter (social.msdn.microsoft.com/Forums/en-US/…)
@MarkSowul what do you mean by "composable"? Also, in my answer i did make the assumption,since the original code for the class backing the variable context was not provided, that the OP was deriving from DbContext. If the OP was using DbContext then my answer works fine (see msdn.microsoft.com/en-us/library/… and msdn.microsoft.com/en-us/library/…). If they are deriving from ObjectContext then your workaround works and i will add it to my answer.
By Composable I mean you can, for example, add a where clause and it will be filtered on the database, as opposed to in-memory. Sorry, entity framework really drives me nuts sometimes and I get cranky about it. I was on a fool's errand trying to get table-valued-functions to work, and ran into the dead-end of CreateQuery which people had suggested for EF4.
This does not work. Database.SqlQuery accept RAW sql, which will be executed on SQL server. SQL server does not know operand "Value" and it has no information about Entities container name (MyEntities), it expects schema name instead.
2

There are two steps:

One, add the reference to the class:

using System.Data.Entity.Infrastructure;

Two, rewrite your code as

var result= ((IObjectContextAdapter)context).ObjectContext.CreateQuery<blah_name>(query);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.