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

DBSet<Entity> does not contain a definition for 'FromSql' #146

Closed
mete-mutlu opened this issue Feb 2, 2017 · 5 comments

Comments

@mete-mutlu
Copy link

@mete-mutlu mete-mutlu commented Feb 2, 2017

Hello,

Im a newbie for entity framework core and ngpsql. I use it to connect Amazon Redshift database.
I want to execute raw sql. But there is no method called FromSql on DBSet. How can i execute raw queries?
Any idea?

Thanks.

@roji

This comment has been minimized.

Copy link
Member

@roji roji commented Feb 2, 2017

The general EF Core docs, which you should go through, are here. If you want to use raw SQL to query entities which are mapped in your context, read this. Otherwise, for doing arbitrary SQL, effectively bypassing EF entirely, you can call ctx.Database.GetDbConnection() on your DbContext to get a plain old ADO DbConnection, construct commands and execute them.

@roji roji closed this Feb 2, 2017
@mete-mutlu

This comment has been minimized.

Copy link
Author

@mete-mutlu mete-mutlu commented Feb 2, 2017

Thank you for the quick response,
I checked the docs you mentioned but i cant see anything that i didnt know. I don't know am i missing a point here? The doc about raw sql already recommends to use FromSql method but my problem is that method doesn't exists on my DBSet. On the other hand i checked ctx.Database.GetDbConnection() that doesnt exist either. Thanks again.

My project.json file

{
  "version": "1.0.0-*",
  "dependencies": {
    "Common": { "target": "project" },
    "Entities": { "target": "project" },
    "NETStandard.Library": "1.6.1",
    "Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0",
    "Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.1.0",
    "runtime.linux.System.Net.NetworkInformation": "4.1.0-beta-23516",
    "runtime.unix.System.Net.Security": "4.0.0-beta-23516",
    "Microsoft.NETCore.Platforms": "1.1.0",
    "dnx-coreclr-linux-x64": "1.0.0-rc1-update2",
    "Microsoft.EntityFrameworkCore.Relational": "1.1.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

My DataContext;

using Common;
using Entities.Concrete;
using Microsoft.EntityFrameworkCore;

namespace DataAccess
{
    public class ProcessedDataContext : DbContext
    {
        public DbSet<ProcessedData> ProcessedData { get; set; }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<ProcessedData>().HasKey(m => m.GUID);
            base.OnModelCreating(builder);
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string constr = Utility.GetConnectionString("DbContextSettings:DefaultConnection");
            optionsBuilder.UseNpgsql(constr);
        }
    }
}
@mete-mutlu

This comment has been minimized.

Copy link
Author

@mete-mutlu mete-mutlu commented Feb 2, 2017

Im sorry:) Microsoft.EntityFrameworkCore namespace was missing in the class where i used my datacontext.

@Yawarmurtaza

This comment has been minimized.

Copy link

@Yawarmurtaza Yawarmurtaza commented Mar 13, 2018

This helped to fix my issue.

@bryan5989

This comment has been minimized.

Copy link

@bryan5989 bryan5989 commented Apr 30, 2019

While the Microsoft.EntityFrameworkCore namespace is needed, the FromSql() extension method is provided by Microsoft.EntityFrameworkCore.Relational Package

At least as of 2.2.4 (didn't check earlier)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.