Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I call a db function that returns a primitive type? #1194

Closed
weitzhandler opened this issue Dec 24, 2018 — with docs.microsoft.com · 5 comments
Closed

How do I call a db function that returns a primitive type? #1194

weitzhandler opened this issue Dec 24, 2018 — with docs.microsoft.com · 5 comments

Comments

Copy link
Contributor

I want to call a built-in db function that returns a bit, how would I achieve that?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 24, 2018

Create a QueryType

 public class BitValue()
 {
     public bool Bit { get; set; }
  }

And get it using FromSQL

@weitzhandler
Copy link
Contributor Author

@ErikEJ Funny but thanks. Would be a great idea to get the results directly for primitive types.

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 24, 2018

Yes, agree on "funny" - making it simpler is under consideration!

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 24, 2018

See dotnet/efcore#11624

Copy link
Contributor Author

weitzhandler commented Dec 24, 2018

For the reference, it's possible using the connection to create a command:

public static async Task<T> ExecuteScalarAsync<T>(this DbContext context, string rawSql,
  params object[] parameters)
{
  var conn = context.Database.GetDbConnection();
  using (var command = conn.CreateCommand())
  {
    command.CommandText = rawSql;
    if (parameters != null)
      foreach (var p in parameters)
        command.Parameters.Add(p);
    await conn.OpenAsync();
    return (T)await command.ExecuteScalarAsync();
  }
}

Would be nice if the docs were updated to include this info and people's life easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants