Stored procedure not appearing in EF code - stored-procedures

I'm using EF database first and have added a stored procedure to the database.
When I ran Update model from database on the edmx file it picked up the stored procedure and I selected it as an item I wanted to include.
I have a file named Model<projectname>.Context.cs with a class called Entities in it. This is an auto-generated class and it contains methods for other stored procedures in the system. My new stored procedure does not have a corresponding method in this class and running Run custom tool does not help.
Is there something else that I need to do, that I am not doing?
Thanks,
Sachin

EDMX doesn't support Table-valued parameter. So if your proc uses the TVP as the parameter, the EDMX designer can't pick it up.
Edited:
If you want to retrieve the result set and create object/POCO for the proc, then just simply remove the tvp, compile the proc, let the EDMX designer to pick up your proc, generate the result object and then add the tvp back to your proc. This is a very easy way to cheat the EDMX designer and get what you need.

Ensure you are doing "Run Custom Tool" on the *Model.CONTEXT.tt file.

Related

Add SQL function into project with EF6

I want import my function from SQL Server into my ASP.NET MVC project with EF6 database first.
My problem is when I update database in .edmx file, based on what I found in the sources, I open Model Browser but in this file I can't use "Add function import" because stored procedure / function name is empty.
What do you think? What is my mistake?
Finally I couldn`t find what was the problem, but I got answer by this solution :
I create an stored procedure and called my function in it. after that I added stored procedure in my project as easy as possible.

Combination of code first and database first approach

Is it possible that we can use entity framework power tool to import pre created stored procedures but this should be in code first approach.
What I am think is that
1) I will create a code first project.
2) With entity framework power tool i will generate edmx from context.
3) I will import my stored procedures.
4) I will provide sql script which will create stored procedure with SQL() function in migration.
5) The next time when a fresh database is used, the procedure will be created from script and it will be mapped with edmx.
Please provide any details about it.

HOw can I access two database in single .edmx file?

I have created mvc3 application.
I have one .edmx already created which is based on Db1 but now
I have created a view which is based on Database2 and I need to use this view inside my project.
For that I need to update my EF .edmx file.
but when I right click and select option Update model from Database
i'm only getting all tables , view ,sps fromDb1` its obvious
But as i need to use view which is fromDatabase2how can i add it into my model.edmx` file?
please help.
If two edmx want to merge then make partial class same for both edmx file (there will be two designer classes). Add another constructor and make it parametrized, for other edmx file. Parameter to identify which edmx want to load.
Add another class file in Business layer create object of edmx partial class in this class file, Under this class when ever you want to load whom so ever edmx file pass some argument in constructor of edmx partial class constructor to identify which connection needs to open.
Pass parameter in constructor of edmx designer class, based on decided page name (custom logic or table name; That edmx will get loaded.
In web config file multiple connection strings will available for multiple edmx file.
Entity Framework does not support mapping more than one database to one model/.edmx file (see See : unify two models (edmx) with visual studio 2010)
So you'd need to create a separate .edmx file/model for the other database, and reference each model with separate contexts. You'll need 2 connection strings in your projects as well.
One "hack" might be, for i.e. MS SQL to link these two servers and expose the data from other one on first one, i.e. via view. But I think it's manageable only for few tables. With huge models this will be pain. Other databases (Firebird, Oracle, ...) support this in similar way.
What I have done , created stored procedure in db A and accessed the db B through that SP , say select * from db2.table.then create a function import for that particular SP .
This approach works well if you have both databases on same server. In case these are on different servers you can create Linked Server on B to access A using the same stored procedure approach.
Using ctx As New Entity()
ctx.Database.Connection.ConnectionString = conString
End Using

Property in Entity partial class

I have an entity/table that uses sqlgeography.
Since EF 4.X doesn't support spatial types I'm instead sending the bytes of the field back and forth.
I have stored procs on the database side that handles the converstion and properties on the code side to do that job.
To add the properties in the code I used a partial class.
One of those properties is for the SqlGeography which simply wraps around the byte[] property to handle getting and setting.
This property is hidden from EF using the NotMappedAttribute.
The other is the property exposing the byte[] itself and is decorated with the EdmScalarPropertyAttribute and DataMemberAttribute.
I then go to the EF model designer (*.edmx) to point the entity model at the Insert/Update/Delete stored procs.
It finds the stored procs alright and realises that they (when appropriate) take a VARBINARY parameter.
It also has a drop down allowing you to select a property on the entity class which maps to that parameter.
However this drop down doesn't list either of my properties. I don't care about the SqlGeography property since that is meant to be hidden from EF, however it is vital for me to be able to point it at the byte[] property, as that is where the data comes from.
I would very much like to avoid database triggers or wrapper classes and addiitonal fields to fudge this in to working.
I tried manually editing the .edmx file to include the byte[] property, but then it just complains it's unmapped.
Can anyone give me some insight in to how to get this to work? Or an alternative method of achiving the end result?
We could use a view to create the binary field for us, but this then involves manually creating a lot of the xml for the relationships within the data.
This pretty much voids the point of using EF which is to make life simple and easy.
For this project We'll just add a binary field to the table then have sprocs to handle the converstion on the server and a property in a partial entity class for exposing the geography type in the model.
Next project I doubt we'll be using EF. Dapper is so much more painless, even if theres a touch more code writing involved.
Here's the links for using views if anyone thinks it would be applicable to them:
http://thedatafarm.com/blog/data-access/yes-you-can-read-and-probably-write-spatial-data-with-entity-framework/
http://smehrozalam.wordpress.com/2009/08/12/entity-framework-creating-a-model-using-views-instead-of-tables/
In the end we created a computed column for each table that exposes the spatial data as bytes.
We then use stored procs for inserting and updating the spatial data.

ASP.NET MVC Partial Model Binding from Stored Procedures

Currently, I have a set of stored procedures which are called to populate different fields on a page. I am calling the stored procedures from the LINQ to SQL class that I dropped them on, which works fine. However, it automatically generates (StoredProcedureName)Result class names which I have to work with.
Is there anyway to take a stored procedure and bind the result to multiple classes? For example I have a stored procedure that gets the information for a blog post. I want to bind that data into a Post class and a User class (for the author), however I am not retrieving all of the data from the stored procedure to fill an entire Post object or User object (the stored procedure only returns the title, content, and author name).
What is the best way to handle this scenario? I am open to changing the stored procedure but I would rather not have to return data that I am not going to use just to populate a full object and I cannot use LINQ to SQL to query the database.
You can create a partial of your DataContext and write a method that returns IMultipleResults, binding to each of your respective classes with a [ResultType(typeof(ClassName))], as shown here:
http://msdn.microsoft.com/en-us/library/bb399371.aspx
Or, you can store the results of the stored procedure in a variable and then LINQ-Select new instances of your target classes like this:
var result = dataContext.your_stored_proc();
var post = result.Select(s => new Post { Id = result.Id, Title = result.Title });
You can use this library:
https://github.com/mrmmins/C-StoreProcedureModelBinding
This classes are using reflection, and you need include it in your project and create an instance of Generic.cs

Resources