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

DBQuery() is obsolete in EF core 3.x #21947

Closed
freetison opened this issue Aug 6, 2020 · 5 comments
Closed

DBQuery() is obsolete in EF core 3.x #21947

freetison opened this issue Aug 6, 2020 · 5 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@freetison
Copy link

freetison commented Aug 6, 2020

[https://github.com//issues/19509]
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

It´s easy to reproduce it.

The idea is to map the SP result into a class, it worked very well in EF Core 2.1.6
I personally do not understand why the team removed it

Base on the same code @ajcvickers used..

public class Blog
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Url { get; set; }
}

public class Complex_DTO
{
	
}	

public class BloggingContext : DbContext
{
	public DbSet<Blog> Blogs { get; set; }  // Represent and entity on DB (a table)
	
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
    }
       
	
	protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

        modelBuilder.HasAnnotation("ProductVersion", "1.0.1");
		
	modelBuilder.Entity<Blog>().ToTable("BLOGS");
        modelBuilder.Entity<Blog>().HasKey(t => t.Id);
        modelBuilder.Entity<Blog>().Property(t => t.Id);
        modelBuilder.Entity<Blog>().Property(t => t.Name);
        modelBuilder.Entity<Blog>().Property(t => t.Url);
		
	//  Custom Query from SP
	modelBuilder.Query<Complex_DTO>();    // 'ModelBuilder.Query<TQuery>()' is obsolete: 'Use Entity<TEntity>().HasNoKey() instead'

        modelBuilder.Entity<Complex_DTO>().HasNoKey();   // EF 3.0 way
            
        base.OnModelCreating(modelBuilder);
        }
}



public class Program
{
    public static async Task Main()
    {
        using (var context = new BloggingContext())
        {
            var blogs = context.Blogs.ToList();
			
                       //The new way in EF core  > 3.x.
			var sp = $"EXEC some_sp @param1 = 2"
			var spResult = context.Database.ExecuteSqlRaw(sp);  // This return just an Int
			
			//The older way in EF core 2.1.6..
			var spResultDto = context.Query<Complex_DTO>()  // Thsi return sp result as Complex_DTO 
									.FromSql(sp)
									.ToList();
			
        }
    }
}

@smitpatel
Copy link
Contributor

What is the issue here?

@freetison
Copy link
Author

There no way to map the sp result into a class.
The team removed this feature and now is to hard to upgrate projects from EF Core 2.1.6 to 3.x

Similar issue was closed without a solution.
[https://github.com//issues/15656]
[https://github.com//issues/19509]

@smitpatel
Copy link
Contributor

smitpatel commented Aug 6, 2020

context.Set<Complex_DTO>().FromSqlRaw(sp).ToList();

@freetison
Copy link
Author

context.Set<Complex_DTO>().FromSql(sp).ToList();
'RelationalQueryableExtensions.FromSql(IQueryable, FormattableString)' is obsolete: 'For returning objects from SQL queries using plain strings, use FromSqlRaw instead.

but this works
context.Set<Complex_DTO>().FromSqlInterpolated(sp).ToList();

Thanks for your help.

@smitpatel
Copy link
Contributor

Updated. FromSql is obsolete in 3.x. FromSqlRaw or FromSqlInterpolated should be used according to use.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Aug 6, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants