So, I (think) tried the same thing, and the only solution I found is using TVF in SQL Server Manager. I accept EF 6.1.1 and Web API. Here are a few steps:
(1) Create a procedure (you can add parameters to it if you want):
CREATE PROCEDURE [dbo].[GetArticleApiKey] @a nvarchar(max) AS SELECT a.* FROM [dbo].[Blogs] b, [dbo].[Articles] a WHERE b.ApiKey = @a AND b.Id = a.Blog_Id
If you need this in Code-First, you can use Initalizer with Database.SetInitializer in your DbContext constructor.
(2) Download this nuget package. It allows you to query stored procedures. Here is the basis of the project.
(3) Add the following to your DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Add(new FunctionsConvention<YourContext>("dbo")); }
(4) Add the stored procedure to your context:
public ObjectResult<Article> GetArticleApiKey(string apiKey) { var apikeyParameter = new ObjectParameter(ApiKeyParameter, apiKey);
(5) Now you can use this function in your controller and perform pre-filtering. Subsequently, all Odata queries are still possible, but only based on the results of the sql procedure.
context.GetArticleApiKey("MyApiKey").AsQueryable();
I hope that I understood correctly what you are asking.
source share