Entity Framework Database First not bringing in functions - c#

Im using vs2012 and have a project where I have an Entity Framework v4.4 model pointed at a Sql Server 2008R2 database. For a while I have been adding stored procs and functions to the database and using Update Model From Database to bring them into the model without any problems.
Recently another developer working on the project started having trouble adding new stored procs and functions - he is using VS2013. When he chooses Update Model From Database, new stored procs show up in the list of procs to add, but when Finish is pressed, they are not added to the model, and in fact if he chooses Update Model From Database again, they are still listed in the new stored procs to add. Same thing happens with new functions.
He was able to get around this by modifying the edmx file manually, but we don't like this solution.
We have looked on SO and seen messages regarding giving EXECUTE permission on the procs (which we have not had to do before this). We tried giving the Execute permission, but that didn't change anything - same behavior.
Any ideas?

The functions were all table based functions, which evidently are not imported in EF 4

Related

Dataset Designer functionality in Asp.Net

I am developing an Asp.Net WebForms website. I am used from my prior project to the "dataset designer", it was my main worksite, I had there all my tables and also hundreds of queries which I could easily call from code like this: "this.InventoryTableAdapter.MyQuery('parameters'), it was great, but I can't find such a thing in Asp.Net, am I missing something. how do you access your special queries in code. do you have to write cumbersome ADO.Net code for each small thing, can someone enlighten me please.
You can still add a database designer to your .NET application. Today's standard ORM for .NET is Entity Framework, although many other good options exist. One thing to research is Entity Framework Database First. Here's how to get started...
In your project, choose to Add New Item. In the Add New Item dialog choose the Data items and then select ADO.NET Entity Data Model. Click Add.
In the Entity Data Model Wizard, select EF Designer from database.
If you have database connections defined within your development environment, you may see one of these connections pre-selected. Or, you can create a new connection to your database.
After setting up your connection, select the tables you want to generate models for. These will be the tables available in your database designer.
This is a very high level how to. You can read more about this approach here: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/creating-the-web-application
Another ORM option is LinqToSql. You can read more about creating a database designer ".dbml" file using LinqToSql here: https://msdn.microsoft.com/en-us/library/bb384428.aspx
I hope this helps.

Entity Framework code first with complex oracle stored procedure

I am working on upgrading an ASP.Net Web Forms application to MVC + EF Web Application.
I am more or less clear about the MVC part but seems to be a bit stuck about the EF part.
Since its a upgrade so there already exists a DB with lot of stored procedures, functions and other things and I would like to keep them.
For EF Code First from DB is the prefered option as it generates the Model classes automatically.
I am struggling to conceptualize how this will actually work.
for example:
I have two tables Employee & Salary as
There is a stored procedure getEmployeeDetails which returns a cursor with properties from both tables.
I am not sure how to map this in DbContext as what I have read so far is that you can map stored procedure to a single mode and not two or more.
Can someone please suggest what can do done here and what will be the best option in terms or using EF code first with existing procedures.
Thanks!!!

C# Is there an easier way to create a database, empty tables, tables with data in them by default, stored procs and views?

Before I posted this question, I did some Googling first on how a database was created through C# and mostly it points to either SMO or SQL query files and it was the time of SQL Server 2005 and 2008.
So at this day in age, is there an easier way to create a database with empty tables, tables with data in them by default, stored procedures and views?
I need a suggestion.
I think the answer is probably Entity Framework. You can do 'code first' and use database migrations, allowing you to write your C# code and use that to generate a lot of the database for you.
Ultimately though, 'easier' is subjective. I personally find EF great for the 'normal' stuff, but at the end of the day, if you need a stored procedure to do some custom logic; you need to write the custom logic, in some fashion.
Maybe have a look and see if you think it fits your needs.
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Looked at the database projects in studio 2013. You create a database as a series of scripts using a familiar GUI. However, changes are published - this process creates a unique change script targeting the connection you define. For new databases the whole thing gets created, but publish against a partial or out dated version and the script created in a change script to bring it up to date.
You can even writ unit tests against your database using specialist tools, although I do find them lacking a bit.
More on msdn - here
Depends. right out of gates. Sp and views. Best shot is directly from database through a workbench. I can then capture definitions and store in a file to be replayed through c#
As for tables there are many orms that can generate tables via c#. Look at entity frameworks. Code first examples
I have generated tables using EF Works fine. I then went into database and created views and sps.
The trick is to migrate new views and sps into your EF model U can google entity Frameworks code first ... Adding views and SPs.
Worst case is u create database all through database workbench. Create a script that an be played to recreate eveything. By running. Then use EF DATABASE first approach
In either case u end up with a good set of autogenerated code to manage CRUD and object management and an abstracted data model

Stored procedures and functions is not updating for Entity Model

I work with database first Entity Framework & LINQ in Visual Studio 2013.
When i write stored procedures and change them, i used to update Entity Data Model from database by choosing stored procedure updating option.
It is weird that the 'import selected stored procedures and functions into the entity model' option comes disabled so I cannot choose stored procedures and functions for updating model anymore..
Can you please help me to find the problem?
You only have to import stored procedure and functions into the model when they're new. So when there are no new ones the option is disabled.
This doesn't mean that, as you seem to suspect, EF doesn't update existing stored procedures and functions. It does.

how to detect the new database when change database in asp.net mvc4

The problem that when i change the new database then application not detected the new database and retreive error
The model backing the 'DBContext' context has changed since the
database was created. Consider using Code First Migrations to update
the database (http://go.microsoft.com/fwlink/?LinkId=238269).
but i don't using Migrations so i don't update-database by Package Manager Console
How can i fix this problem?
Actually it does detect changes in your database. The database differs from the model. The error message.
This error is thrown when the hash stored in the migrations history table does not match a calculated hash. (Have a look at __MigrationHistory table, might be under system tables)
If you delete this table the check is essentially disabled. You can achieve something similar by adding Database.SetInitializer<YourContextType>(null) to the startup of your application. The latter disables database creation from within that application, so if you want to create the database by code, you would need to do this from a separate application (for example an console app). I prefer to go this way for web applications.
Secondly: if you change your database manually (change columns, add tables, etc.) you need to adjust your model. So for each DDL statement, change your code.
If you are not using code first, you could update your existing model in the designer.
I ran into this problem when I first started with code first and mvc. the answer below is absolutely correct but you should go to the ASP.net website and do some tutorials on code first migrations. you need a better understanding how update database and initialize and migrations work.

Categories