3

Why I can't see the stored procedure added in my DbContext? The DbContext is generated by the template introduced with the CTP5 release (with POCO classes).

I added the stored procedures as said by this tutorial: http://thedatafarm.com/blog/data-access/checking-out-one-of-the-new-stored-procedure-features-in-ef4/

Moreover I searched if the entry is added in my context and these are the results:

<Function Name="GetClientsForEmailSend" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" />

<FunctionImport Name="GetClientsForEmailSend" EntitySet="Client" ReturnType="Collection(DBMailMarketingModel.Client)" />

<FunctionImportMapping FunctionImportName="GetClientsForEmailSend" FunctionName="DBMailMarketingModel.Store.GetClientsForEmailSend">

A similar question is:

Why won't EF4 generate a method to support my Function Import?

but I've done all the suggests.

This is the stored procedure:

ALTER PROCEDURE GetClientsForEmailSend
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT OFF;

-- Insert statements for procedure here
SELECT *
FROM dbo.Client AS c
INNER JOIN Subscription AS i on c.IDClient = i.IDClient
WHERE c.Email is not null and c.Email <> '' and i.Active = 1
END
GO

What I'm missing?

Thanks

Community
  • 1
  • 1
stuzzo
  • 1,056
  • 1
  • 15
  • 36

1 Answers1

5

DbContext doesn't provide support for mapping stored procedures to methods. You must call procedure directly by using context.Database.SqlCommand(...) or context.Database.SqlQuery(...)

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for the answer. You're right, but unfortunately I don't received the exptected result.The result that I want is to load Clients with Active Subscriptions(that has a bool active),so if a client has 3 subscriptions, but only 2 are active I want that the dbcontext loads only the latters.I tried with the sp above and the variable that store the result is null and also with LINQ var result = from e in clients let sub = e.Subscriptions where sub.ToList().FindAll((s) => s.Active==true).Count>0 select e; Can you suggest me a solution ? Thanks! – stuzzo Feb 18 '11 at 15:25
  • @stuzzo: Loading parent entity with filtered childs must be done with projection. Check this question: http://stackoverflow.com/questions/4720573/linq-to-entities-how-to-filter-on-child-entities – Ladislav Mrnka Feb 18 '11 at 15:39