3

I have an existing app that I am trying to upgrade from MVC5/EF6 to MVC6/EF7. We dynamically create some of our SQL tables, and as a result, have made use of the

System.Data.Entity.Database.SqlQuery

method to automatically map to entities that we use throughout our application.

This method seems to have gone away (i.e. not part of Microsoft.Data.Entity.Infrastructure.Database ) in EF7 (or is not yet implemented). Are there plans to re-implement this method in EF7 or is there another way to accomplish this? Our project is kind of dead in the water until we figure this out.

Edited on May 20, 2015

I've been trying to make this work with FromSql, since that's what's available in Beta4, but no matter what combination of concatenated string, parameters I try, I keep getting different versions of an "Incorrect Syntax near @xxxvariable" message.

var results = Set<AssessmentResult>().FromSql("dbo.GetAssessmentResults @FieldA='Data1', @FieldB='Data2', @UserId = 2303"); 
var results2 = Set<AssessmentResult>().FromSql("dbo.GetAssessmentResults @FieldA= {0}", intData); 

Both of these calls result in

"Incorrect syntax near '@FieldA'" 

Any ideas?

1 Answer 1

2

We recently introduced the .FromSql() extension method on DbSet. It has the added benefit that you can continue composing LINQ on top of it.

var customers = db.Customers
    .FromSql("SELECT * FROM Customer")
    .Where(c => c.Name.StartsWith("A"));
2
  • Thanks for the reply! Is that a part of 7.0.0.0-beta3? Or is it even newer than that? Also, the SqlQuery method can specify a Type as its first parameter. I wouldn't have a DbSet available to be able to use this method. I currently dynamically create each Type using Reflection. Is there a way I can use what you mentioned above with this sort of approach? Again, I really appreciate your input. Thanks!
    – Hayaku77
    Apr 2, 2015 at 16:53
  • FromSql is not available in beta 3. For the time being, FromSql is the only way we can run plain SQL statement.
    – Ricky
    Apr 21, 2015 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.