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

Enable flexible FromSql mapping #21627

Open
Tracked by #21888
AndriySvyryd opened this issue Jul 14, 2020 · 5 comments
Open
Tracked by #21888

Enable flexible FromSql mapping #21627

AndriySvyryd opened this issue Jul 14, 2020 · 5 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jul 14, 2020

This builds upon #17063, #13358 to enable different mappings for different SQL queries and to allow parameter configuration. This would also support JSON columns.

It would be configured like so:

modelBuilder
  .Entity<Blog>()
  .HasQuerySql("GetDeletedBlogs", "select id as BlogId, name as BlogName from Blogs where Deleted={0}",
    q =>
    {
        q.Propety(b => b.Id).ToColumn("BlogId"));
        q.Propety(b => b.Name).ToColumn("BlogName"));
        q.Parameter("0").HasType("bit"));
    });

And used like so:

var deletedBlogs = context.Set<Blog>().FromNamedSql("GetDeletedBlogs", true).ToList();

or

var deletedBlogs = context.Set<Blog>().FromNamedSqlRaw("GetDeletedBlogs",
  "select id as BlogId, name as BlogName from Blogs where Deleted={0}",
  true)
 .ToList();
@ircnelson
Copy link

In your second example, you did mean?

var deletedBlogs = context.Set<Blog>().FromNamedSqlRaw("GetDeletedBlogs",
  "select id, name from Blogs where Deleted={0}",
  true)
 .ToList();

I suppose that Blog class have Id and Name properties, and should be filled by convection through sql fields and class property's names, correct?

@AndriySvyryd
Copy link
Member Author

@ircnelson No, the example shows that column names can be customized for raw SQL queries without affecting the table to which the entity type is also mapped.

@AndriySvyryd
Copy link
Member Author

Also add a note to the CustomQueryMappingOnOwner exception suggesting to use named queries

@smitpatel
Copy link
Member

Also consider API to mark the SQL as non-composable so we would run it as-is.

@AndriySvyryd
Copy link
Member Author

AndriySvyryd commented Jul 28, 2020

Related: #21827, #21660

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants