Can Entity Framework be extended to add new types of queries to the database?

Is it possible to extend EntityFramework (like v6) to add support for a new SQL feature that EF does not currently support, perhaps writing extensions to a library. We are using MSSQL and don't need cross-db capabilities. We call a C # method that builds a SQL statement and then calls ExecuteSqlCommand()

to execute it. This is obviously very ugly and I was wondering if EF could be extended to add a completely new query capability such as MERGE

.

We will need the entire LINQ query to generate IQueryable

, for example, EF, and defer execution until needed, such as, for example, Context.MyCollection.Where(x => x.Num == 6).Select(x => x.ID)

it will execute until the actual value. For example, for example, when calling.ToList()

As a side question, if I could extend EF to add this capability, it would be possible to extend the LINQ query language. Example:

from s in Context.Schools
where s.District.DistrictName == "District 5"
merge os in Context.OtherSchools using (os.SchoolID == s.SchoolID) into m
when matched Context.OtherSchools.Update(s)
when not matched Context.OtherSchools.Add(s)
select m.inserted.id

      

Or something like that ... I know many of them don't exist, but I was wondering if it is possible to extend linq to add keywords to it? If not, is it even possible to extend EF to add new features like SQL implementation MERGE

or others in lambda style? And if possible, where would you even start looking for this functionality?

+3


source to share


1 answer


This is an old question, but no one seems to have been answered yet.

It would be impossible to extend the Linq language itself. It's built into the C # compiler and I don't think it's extensible. Those. you cannot enter a keyword merge

there.

As for extending the query provider for EF itself, I'm not sure, but this is what I'm looking for myself. This will mean that you can make queries using method syntax instead of Linq.



There are some hints about the possibility of registering "services", but I haven't been able to figure out how to actually implement a derived query provider or anything that would convert new expression nodes to SQL while still using the existing provider's implementation for everything else. ...

So far, I would say this is not possible with EF6 (and if it is, I would say it is not well documented and no man's land).

0


source







All Articles