sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
707 stars 228 forks source link

Add custom entity to context for running raw sql #722

Open hstridh opened 3 years ago

hstridh commented 3 years ago

Hi,

I trying to run a raw sql statement and map to an entity as describe here: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

But I would like to map to a custom entity so that I can do: var customEntities = context.MyEntity .FromSqlRaw("{AComplicatedSqlStatement}") .ToList();

But how can I add a custom entity like below to the context? Is there a way to specify this in the database.tt file?

public class MyEntity { public string a {get;set;} public string b {get;set;} public string c {get;set;} }

ericoldre commented 3 years ago

You should be able to define your MyEntity in a separate file (MyEntity.cs). Then, if you make your DbContext a partial class

Settings.DbContextClassModifiers = "public partial";

This will allow you to add more functionality to your DbContext class in a separate file that is not generated.

partial class MyDbContext
{
    public DbSet<MyEntity> MyEntities {get;}
}