I am aware that table valued functions are not supported in previous versions of entity framework. I was wondering if this is now supported in EF 4? I cant see my functions in the edm designer so i'm guessing they are not supported unless I am doing something wrong?
If they are not supported is there a workaround? My table valued function takes a single parameter.
Yes, you can definitely use Table-Valued functions with EF. Here's a great link on how this can be achieved: http://blog.ondrejsv.com/post/Using-table-valued-database-functions-with-Entity-Framework-40.aspx#comment
It appears as you can use them...
Link
It uses the GetFriends() table valued function. Still i don't really know how to use it.
Related
I'm moving from EF Core 2.2 to 3.1. One breaking change (#15392) was that it no longer composed over stored procedures, so you had to add 'AsEnumerable'. That usually works, but I have a stored procedure call on a TPH table where that fails:
My call to the SPROC is:
SqlParameter authorizedUserID_p =
new SqlParameter("#authorizedUserID", authorizedUser.ID);
IEnumerable<Post> query =
context.Posts.FromSqlRaw<Post>("Post.USP_ReadPost #ID, #AuthorizedUserID",
parameters: new[]{ parentID_p, authorizedUserID_p }
).AsEnumerable<Post>();
Post targetPost = query.ToList<Post>().FirstOrDefault<Post>();
And it produces this error, recommending using AsEnumberable (which I'm already using above):
System.InvalidOperationException: FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it.
Consider calling AsEnumerable after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.
I believe the reason is because my Posts table is Table-per-hiearchy, as other calls to SPROCS in the same application are working fine. Would appreciate any help possible!
This is yet another issue introduced by EFC 3, tracked by #18232: Impossible to use stored procedures related to entities that inherits another one.
The reason is that SP calls are not composable, and EF Core always try to compose SQL for TPH base entities in order to add discriminator condition. Similar to Global Query Filters, but there you can at least use IgnoreQueryFilters, while here you have no option.
The good news is that it's already fixed in EFC repository. The bad news is that it won't be released until EFC 5.0.
Since AsEnumerable() doesn't help, all you can do is to wait for EFC 5.0. Or, if possible, convert SPs like this to TVF (table valued functions) which are composable. In general, use scalar functions or stored procedures with output parameter(s) for non query returning calls (to be executed with ExecuteSql*), and table valued functions for single query returning calls (to be used with FromSql*). Note that currently EFC does not support multiple query returning stored procedures anyway.
Hi I have a project which is quite old. It uses stored procedures which isn't an issue but it does have lots of error prone mapping code in old ADO.net. My main goal is to cut down the mapping more than anything. We have a separate library of I guess POCO/DTO type objects that get mapped to.
Can I map stored procedures to these classes easily. I suppose I could use AutoMapper as the mapping will be exact but I'm wondering if there's an easier way that it does out of the box. The list of complex type doesn't show the imported library's business classes. Cheers.
Instead of using AutoMapper, create a complex return type using "Edit Function Import", which has the exact properties as the stored procedure result.
Thanks for taking a look at my post.
I've been working with linq to sql and have generally been happy, until i just noticed that database table names are hardcoded into the classes/dbml files-- which can't work in our environment. We need to be able to have database names completely changeable via web.config-- in one place. That's a definite requirement.
Do you know how this can be achieved with Linq To Sql? If not, does the Entity Framework behave in the same manner? Perhaps i will have to port my model.
Appreciated!
In other words, i need "Tourism_DB" in the DataContext file:
[System.Data.Linq.Mapping.DatabaseAttribute(Name="Tourism_DB")]
public partial class TourismDataContext : System.Data.Linq.DataContext
{
as well as this text in the dbml
<Database Name="Tourism_DB" Class="TourismDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
to NOT be used, and the value in the web.config to be used anyway.
http://weblogs.asp.net/rajbk/archive/2008/05/20/connection-strings-in-linq-to-sql-classes.aspx
note you can do this with LINQ to SQL:
Dim c As New MyClassesDataContext(ConfigurationManager.ConnectionStrings("ConnectionString1").ToString())
The method i found here worked the best http://msmvps.com/blogs/superska/archive/2009/03/13/linq-to-sql-connectionstring-in-web-config.aspx. It laid out the full process.
I am using the latest beta of Visual Studio 2010 and the Entity Framework. It's mostly really neat, but here's my situation:
I have a table T with columns Id and Name.
I have an auto-generated entity with Id and Name properties.
Finally, I have a stored procedure that selects only Id from T.
Trying to map my entity to the stored procedure results in an EntityCommandExecutionException:
A member of [the entity], 'Name', does not have a corresponding column in the data reader with the same name.
That makes sense, but is there some way to partially populate my entity from the stored procedure call and then fully materialize it later with a second query?
Nine months ago, the answer to this question appeared to involve a great deal of manual labor. In my case, we have hundreds of stored procedures, and literally none of them return full rows. The Entity Framework has come a long way since then, so I am hoping something might've changed.
Thank you in advance for any help!
One approach might be to map the procedure results into a complex type, and then customize the code generation to add a method to this type which will materialize the entire object.
One possible hitch with this idea is that I'm not sure it's possible to customize code generation for complex types. You can certainly customize code generation for entity types, as explained in great detail in this post. It seems like you should be able to customize complex types, as well, but I've never tried it.
I'm new to the Entity Framework.
I have a SQL Function that returns the age for a given birthday.
(http://www.sql-server-helper.com/functions/get-age.aspx)
And I want to use it with linq. I know it's possible. I've read it here:
http://blogs.msdn.com/efdesign/archive/2009/01/07/model-defined-functions.aspx
http://blogs.msdn.com/efdesign/archive/2008/10/08/edm-and-store-functions-exposed-in-linq.aspx
but it seems this is part of the EF extentions (which I have) or that it is still in development.
Can anyone help me with this?
Thank you.
Model defined functions are not in the shipping Entity Framework, and I don't think they are in the extensions, either. As far as I know, they are a new feature planned for .NET 4.0. I don't know if they are in the Community Technology Preview of that. You can see a demo of model defined functions if you watch the Entity Framework presentation from PDC. But they are certainly not in .NET 3.5 SP1.
As a workaround, you could define a computed column on your table which used the function and then map that column instead of mapping the function directly.
You might be able to do a "FunctionImportMapping" see msdn for more info..
You could definitley do it that way if you exposed it as a sproc, I've not tried with just a function tho..