Raw SQL Query without DbSet - Entity Framework Core

后端 未结 17 1198
眼角桃花
眼角桃花 2020-11-22 13:27

With Entity Framework Core removing dbData.Database.SqlQuery I can\'t find a solution to build a raw SQL Query for my full-text search query th

17条回答
  •  长情又很酷
    2020-11-22 13:47

    In EF Core you no longer can execute "free" raw sql. You are required to define a POCO class and a DbSet for that class. In your case you will need to define Rank:

    var ranks = DbContext.Ranks
       .FromSql("SQL_SCRIPT OR STORED_PROCEDURE @p0,@p1,...etc", parameters)
       .AsNoTracking().ToList();
    

    As it will be surely readonly it will be useful to include the .AsNoTracking() call.

    EDIT - Breaking change in EF Core 3.0:

    DbQuery() is now obsolete, instead DbSet() should be used (again). If you have a keyless entity, i.e. it don't require primary key, you can use HasNoKey() method:

    ModelBuilder.Entity().HasNoKey()
    

    More information can be found here

提交回复
热议问题