EF Core 2.0 and raw queries

I'm sure I did something similar in EF Core 2:

// Not in Db
public class MyCustomProjection
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class MyDbContext: DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    [NotMapped]
    public DbSet<MyCustomProjection> dummy { get; set; }

    public List<MyCustomProjection> GetCustomProjectedBlogs()
    {
        return dummy.FromSql("SELECT Id, Title AS Name FROM Blogs").ToListAsync();   
    }
}

To query FromSql in a custom projection, I added a DbSet with my projection to my DbContext and added the [NotMapped] annotation.

I'm sure this worked, but now it seems that if I use the NotMapped annotation, it will create the table anyway, and if I use the Fluent api Ignore with the entity, the table will not be created, but EF will create an error saying you cannot project onto ignored object.

I no longer have access to my old code (in another company), and I cannot recreate this bit. Someone please explain if the memory is corrupted or if I am doing something wrong.

+4
source share

Source: https://habr.com/ru/post/1695998/


All Articles