4

So I'm migrating from .NET Framework to .NET 6. I'm in a function where the guy I took over from spends line 693 - 760 generating a query in SQL and getting the result with this:

var result = db.Database.SqlQuery<CustomObject>(sqlQuery.ToString(), parameters.ToArray());

But I am very kindly being informed that:

'DatabaseFacade' does not contain a definition for 'SqlQuery' and no accessible extension method 'SqlQuery' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)

I am using System.Data.Entity as the docs say. I know I can do db.[TableName].FromSqlRaw, but I can't use that because the object I need isn't in the database.

I've got the following packages but I may be missing something?

EntityFramework (6.4.4) Microsoft.EntityFrameworkCore (6.0.7) Microsoft.EntityFrameworkCore.Design (6.0.7) Microsoft.EntityFrameworkCore.Relational (6.0.7) Microsoft.EntityFrameworkCore.SqlServer (6.0.7)

6
  • If only there were working examples somewhere on the internet. Just to point out EF6 is different to .Net 6. learn.microsoft.com/en-us/ef/ef6 vs learn.microsoft.com/en-us/ef/core/get-started/overview/… Commented Aug 8, 2022 at 13:57
  • 2
    You have referenced both EF6 and EF Core 6, you need to figure out which one you are actually using (I would guess the latter) and then you use db.Database.ExecuteSqlRaw for example, which you probably would have found if you have looked for it. Commented Aug 8, 2022 at 14:02
  • You can use this with EF Core nuget.org/packages/… Commented Aug 8, 2022 at 14:04
  • @DavidG I had found those, but they return the amount of rows affected and not what I need Commented Aug 8, 2022 at 14:31
  • You need to use Keyless entities, but first you need to fix your project up a bit. You have referenced both Entity Framework 6 and EFCore. Remove EntityFramework 6.4.4 from your project. learn.microsoft.com/en-us/ef/core/modeling/… Commented Aug 8, 2022 at 14:37

1 Answer 1

6

So I recently went through the same process migrating from EF to EF Core.

If your Entity is NOT in the database, and is just based on an SQL query, you can use Keyless entities and map to a NULL view. Docs: https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=data-annotations

  1. Add your entity class (which should match the result from query)

    public class Person
    {
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }  
    }
    
  2. Add the collection to your DbContext as a DbSet:

    public class FooContext : DbContext
    {
        ....
        public virtual DbSet<Person> Persons { get; set; }
    }
    

    To make the entity keyless, you can either use the [Keyless] attribute, or in Fluent syntax in DbContext:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
                 .Entity<Person>()
                 .HasNoKey()
                 // ToView should be NULL, or you can Map to an SQL View here
                 .ToView(null);
    }
    
  3. Then you can query the Persons like:

    var paramss = new[] { new SqlParameter("@foo", 123) };
    
    fooContext.Persons.FromSqlRaw("SELECT Name, DateOfBirth From Persons", paramss).AsEnumerable();
    
Sign up to request clarification or add additional context in comments.

4 Comments

This looks promising. It'll be a little while until I can see it in action, because good lord there's a lot to migrate here :v
@JaDa yes took me about a week to migrate our solution. But here is some docs I followed: mikee.se/posts/migrating_from_ef6_to_ef_core
well it looks like it mostly worked. I had to give some fields a [NotMapped] and now it's complaining that "Data is Null. This method or property cannot be called on Null values". Fun times, as they say!
Just call me hackerman. I added a "DTO" version of that object where I made all fields nullable. And on that a Map function that turns them into the non-DTO. Turns out that works :v

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.