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.
Related
create procedure GetCityArea
as
begin
select * from CityMaster;
select * from Area;
End
This is my stored procedure GetCityArea which returns multiple record.
My exact problem is I am getting multiple record from the stored procedure, and I want to show this result in single view as separate tables. I am new in ASP.NET MVC and I want to solve this problem using Entity Framework. Please help.
You can do this using ObjectContext and the Translate method.
The gory details are here: https://msdn.microsoft.com/en-us/data/jj691402.aspx
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.
I just wanted to know what is the best practice to create and execute a stored procedure.
I have seen like below:-
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Foo')
DROP PROCEDURE Foo
GO
CREATE PROCEDURE dbo.Foo
But I am thinking when we execute this stored procedure, it will drop the stored procedure and create a new one. But if there is error in the create stored procedure syntax, it won't recreate the stored procedure, right? So as a result our existing stored procedure is deleted and new stored procedure is not created. So what is the real reason for adding DROP here?
The reason is so that your scripts can be run in an idempotent way - they can be run as many times as needed, with the same result. Namely, your database will have the stored procedure you desire. Your procedure will be created, and dropped beforehand if needed. If you didn't do this, then you'd need separate drop and create scripts.
If you're concerned that your scripts have errors, well, fix them. Run your scripts a few times, and fix any problems that arise. The effort is worth not having to maintain separate sets of scripts.
BECAUSE it is execute in line by line manner so when it create procedure and then error comes and again you recreate that procedure then it gives error because it is already generated in your database with error code....
so for this it want recreate that procedure again.....
you can use alter instead of create....
Hope this help you...
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.
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