.Net MVC Database first model with stored procedures - c#

I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!

If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:
this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.

Related

Entity Framework create tables or fields if using dbfirst

The project uses dbfirst approach. On my database I create and edit tables , Then I update the models from the database (edmx).
Is there a way to solve the problem when my models are more relevant than the customer base? It is very important to solve this with the help of EF.
That is, I need to in the customer database automatically added new fields in the tables and created the tables themselves if they were not
You can use Visual Studio over your database model through the designer interface and them updated your database. However, it's a procedure i don't recomend you to do, because depending what operations you want to perform on your database after being applied can rise errors related with some specific types and other specific things and then the roolback could not be so easy.

MVC with EntityFramework model as stored procedures

We have an Asp.net Web application with a normal Ado.NET Oracle connectivity.
The back end works completely on stored procedures.
Now, our management has asked us to upgrade the entire application to MVC.
The management has come up with some standards where they say we must use EntityFramework model and go with the same set of stored procedures without any change.
Here is my question. Each action in my application is running from stored procedures written in the Oracle DB. Is it possible for me to call the exact same stored procedures from Entity framework in Oracle. How can I achieve this?
The stored procedures does plenty of things in the back end like insert,update, select or all in most of the cases.
I know entity framework needs an Entity model. If stored-procedures
will work, what will be the entity model. Is there any workaround for
this?
Try out this example for using stored procedure with Entity Framework, maybe you need to understand the things in your project, whether to use entities or complex types as part of your stored procedures.

Entity framework code first for the subset of DB tables

I would like to add new module (project) to my solution and to use entity framework code first only for subest of my database tables. I'm using ADO.NET with stored procedures in other modules. I plan to split tables from db in the future, but for now it is not possible (tables have no relations to other tables but are used by old modules) I'm not sure if it is good practise to do it in this way and I would like to ask for help.
is it possible to use EF code first for subset of the tables of my DB?
how to initialize these tables with code first? I found only solutions to drop whole
db if model doesn't match and recreate new DB. I need drop and recreate only
tables that are used in my project
is it good practise to use more approaches of the db access to one db?
do you see some problems in this approach? Now I see problem with concurency and data consistency ( if old module will operate with this tables in another approach )
Thank you.
1) Yes, it is. On one of our projects, we had database with store procedures which we migrated to use EF. But not at once. It had taken some time so we used Store procedures whit ADO.NET as well as EF together.
2) I must say I'm not sure about this. We had database already created with only few changes. But you could created tables by yourself.
3) I think better would be to call stored procedures from EF and use it on whole projects if you need them. But using both, ADO.NET and EF is ok, if you have reasons.
4) Why it would be problem if you will use transactions?

How to create a database with Visual Studio, also using UI?

I made the UI side of the project, but now I need to have a database. How can I create it with Visual Studio 2010? I don't have any idea about how to retrieve data with C#. I know SQL but can you give me some examples of that (C# with SQL)?
What Joe talks about and what the post by Scott Hanselman describes is called "Code First". With Entity Framework, there are two other (easy) ways to create a database with Visual Studio 2010 tooling.
One would be to use a Entity Data Model (.edmx) file to create your model (classic Entity Relationship model) and then right click on the model and choose "Generate Database from Model", connect to SQL server and you're done. This is called Model First.
The other technique, which is called "Database First", is when you have an existing Database (or you can create your database directly from SQL Server), and you create an Entity Data Model based on that (you actually get to choose between Database first and model first from the Add-> Entity Data Model dialog). You select the tables, views and stored procedures you want to add, hit finish and you're set.
in both cases, When you build your soultion, you get a data context class that you can use to access your data which is pretty straightforward as well (pretty much the same way as described in scott's blog post).
There are lots of ways to do this, something you might want to take a look at is EF code first where you can create models in code and then generate the database based off of those models.
http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx

ADO.NET Data Services

I'm looking at using ADO.NET Data Services with silverlight. I've been looking at the
Data Model and Data Service Implementations (ADO.NET Data Services/Silverlight) topic on msdn. In their example, they build a data model generated from a database, and query entities in the datamodel. In my case I want to use stored procedures, so I"ve created the data model and added stored procedures instead of tables & views, but now I'm not sure how to execute them. Does anyone have a sample?
Just specify the stored procedures when you build your Entity Framework model. There's lots of documentation about this. You can also see a screencast.
As a point of clarification, I don't think there is anything Data-Services specific about your question. It's more of an Entity Framework question.

Categories

Resources