Entity framework query for MVC - database

Im very newbie to the .net world... I'm going across some tutorials for MVC, where in I could see all the tutorials are writing the Model classes then creating the database(may be used for tutorial sake). But I have the question for the real projects we dont generate the Models and then create database.. rather we do generate the Models according to the database.
So after so much googling and reading different blogs I came to know that we can use ADO.Net Entity Data Model to generate the DataContext. Also I came to know that we need not write the models manually because Entity Data Model generates all the Model classes according to the database.
Can anyone tell me that for real world projects do we use the ADO.Net Entity Data Model to generate the classes and we dont need to write any Model classes on our own.
Please correct if Im wrong...
Thanks in advance

Exactly. You can extract model classes out of your existing database.
For more on this subject, take a look at this blog post:
EF 4.2 Model & Database First Walkthrough
This is the important part in the post linked above:
"... if you are mapping to an existing database you would now select
‘Generate from database’, follow the prompts and then skip to step 4.
"
This is the article that'll show you a complete MVC app created using the Database First approach:
Building an MVC 3 App with Database First and Entity Framework 4.1

Related

Entity Framework, WPF and Asp.Net MVC, structure overview need

Our old application had a middle tier which held business logic and talked to the db, it then talked to 2 clients, one Winforms and one Asp.Net, neither of which knew anything about what kind of database it was speaking to. It used DataSets and DataReaders.
What's the modern equivalent? Are tiers still needed?
There's plenty of questions and answers out there but they all seem a little dated. I'm getting the impression that it should be a tier with Web API and the entity framework which talks to a separate WPF client and something like a Angular JS client.
I'll happily buy a few books if someone could suggest some
For anyone else new to the entity framework. The difficulty here was that the web is full of advice that was correct at the time but becomes out of date. So, for EF6...
There are posts online in which Self tracking Entities were advised (Aug 2013), e.g.
Entity Framework not saving modified children
however they have since become deprecated.
https://msdn.microsoft.com/en-us/data/jj613924.aspx
A page that covers the valid approaches is:
https://msdn.microsoft.com/en-us/data/jj613668
which mentions: Web API, WCF Data Services and also a 'roll your own' approach. The latter is covered in the book 'Programming Entity Framework: DbContext by Julia Lerman'.
Assuming you go down the Web API approach there's an example here:
http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-1

Where do report classes go in a layered design using Entity Framework and Visual Studio 2015

I am using Visual Studio 2015 with Entity Framework 6 and .NET 4.5.2 for a WinForms application. I am currently working on adding reports to my project. I thought about adding the classes which return DTO collections intended for reports to my DataServices section. I decided not to because all classes in my DataServices section currently use my DAL to provide CRUD operations through repositories. My classes for reports will only return read only DTO collections. Since my report DTO classes will not use the typical repository methods (i.e. Add, Update, Delete...) I decided to create a new folder in my project called ReportServices. I have therefore placed my report class named RequestReports (this is for my Request model entity and will return a RequestDetailDto collection) in that folder. Below is a picture of how I have things setup now:
I am not sure if the way I have it setup is the right thing to do. One other way might be to just scrap the idea of a reporting section and mix the reporting classes in with the other data services which use repositories. If I do this, then I would simply not implement the repository methods of add, update, delete for the report classes. I would only implement the get operations for the reporting classes of FindByID and FindAll. Is this really the way to go?
So my question is, "Where should the classes which retrieve information for reporting purposes be placed in a Visual Studio project of a layered design?"
Thanks in advance.
I recommend creating a separate project altogether for reporting. Keep the reporting model and data access separated from your main domain, don't use the same Repositories. Different purposes means different efficient ways of fetching data. It will also pave the way for the option of storing reporting data in a distinct database.
You might want to check out the CQRS approach, it brings a lot to the table on these issues.

Setting up Breezejs with Entity Framework 6

I hope that someone can help me out here. I have just started working with angularjs and breezejs. I am coming from a more traditional MVC 5 and Entity Framework 6 background with this.
I have been working through the Breezejs documentation, in particular how to get the meta data configuration working, but I have to say this is where the documentation is vague. The grammar used refer to 'we', and this is where I (English is my 3rd language), struggle. Do they mean, we the developer working with it in the end, or we the breezejs team. Confusion sets in when I simply cannot find a working example of this either.
So far I have created an empty web application, and installed the nuget packages for breezejs ef, angularjs and hot towel. I also created a separate efmodel project and created code from database. This is now where I get stuck.
Can someone please point me to an example, or help me out how to get it wired up so that my breezejs uses my ef6 model meta data.
Much appreciated.
To answer your question in regards to the words that they use - they are referring to the author of the document and the read together as 'we' meaning that when they say we need to add a metadata endpoint on the WebAPI controller or something that you need to do it and follow along with how they are suggesting.
As far as samples of using Angular with EntityFramework 6 and Breeze just go to their samples site which has a plethora of sample solutions that should point you in the right direction.
http://www.breezejs.com/samples
Good luck!

Catel + EntityFramework

I'm just a starter and asking a simple question.. Just need a direction where to move.
Can someone explain in very few words the process of application development using WPF+MVVM pattern (using Catel) with database created with Entity Framework.
1. I create models (Code First) and generate database
2. I create View models via Catel base classes
3. I create Views with WPF
The question is: how to connect database with Catel View Models? Where to load DBContext and entities? Should I create repositories? Or maybe it's needed to create separate Models?
Is there a common way or maybe example of a small WPF application which uses database to store data?
Sorry if my question is slightly heretic - simply can't sort all the terms and approaches into a single system which I will follow during app creation...
Though there are technical discussions in the field what is best way, there are two commonly used scenarios:
1) Using repositories (Catel does support implementations of repositories for both EF5 and EF6)
2) Use a service that will handle the functions and call that in the view model like any other service.

Data Access in ASP.NET MVC 3

What's the recommended way of creating a Data Access layer in real-life ASP.NET MVC applications? Is it EF? Ef Code First? NHibernate? Any other idea?
Thank you & regards
There is no recommend way.
I prefer EF Code First for doing this because
the Sourcecode is very clean and easy to read
It is easy to simple change the connectionstring and generate a new database if not already exists
easy to migrate your Database if your Model has changed (no need to recreate the Database) using EntityFramework.SqlMigrations
if your Database already exists you can use the Entity Framework Power Tools to generate the Models and Database context from your existing Database
It all depends of your need:
NHibernate is a more mature ORM with a big community
Entity Framework is now getting very well supported and you can find also great information (check Julie Lerman blog)
Both support code generation from database.
Code first: allows you to create your database schema from your domain models. This is great if you don't want to bother with database. NHibernate can do it also.
To help you decide which way to use Entity Framework
Source
There are many other great ORM:
Subsonic
Stackoverflow's ORM Dapper if performance is an important criteria there is a small benchmark on the site
If you're partial to the MSFT tooling and wanting to be "modern", then EF Code First is probably the place to start. One example worth perusing: https://github.com/NuGet/NuGetGallery .
I like to use Model First because it allows me the most freedom to design and implement in my opinion. It also makes it very easy to change the database design.
There is none, stackoverflow is full of people who have gone down the various routes, so you can get help no matter which choice you make.
Best advice, try doing a few small exploratory webs using a couple of approaches which seem to stand out.
The MVC gurus (the people who wrote MVC) at MS all seem to use Entity Framework at the moment. having said that you can use any ORM (or really any data access tech you want) as MVC doesnt actually specify anything at all about the way you access data
Try LINQ, most MVC products I see make use of LINQ.

Resources