简体   繁体   中英

ASP.NET MVC4 with Entity Framework

I'm designing an ASP.NET with MVC4 web application for a multi-tenancy application with different databases for different customers.

I'm planning to use Entity Framework and as I'm new to it, I have the following questions with the assumption that I follow database first model.

  1. Is the EDMX file still needs to be generated? In other words, is the entity model generated in EDMX sufficient or is it required to create another layer of entity models and map it to the database?

  2. Where should the entity conceptual models reside in my application architecture? Can I have all my models (DB models, view models) in a separate project?

Multitenancy would be the same database with a parent "Customer" object or something similar, are you building a multi-instance app (not sure if it's relevant to your question but an important distinction based on how you're deploying the app)?

To answer your other question(s) as simply as possible:

  1. Yes, you need to generate your data-model and you can put them wherever you want. This may be sufficient for your needs but generally you would want to build some custom view models as well.
  2. If you need multiple projects to have access to the same models and you only want to maintain them in a single place, extract them to their own project. If the entire app will be contained within a single project, just drop it into a "Models" folder.

Entity Framework has come a long way from the days of competing with NHibernate and EF getting that "vote of no confidence". 4.x changed the ballgame you end of with many choices, unfortunately a lot of articles reference 4.x and so while you are using MVC 4 , you most likely nuget down the 5.0 version, and some of the examples are not so fun to get up and running.

Julie Lerman wrote a lot of the books on EF, Dbcontex, EF Code First.

You can skip creating an EDMX file, pros and cons to doing this. If you are the hard core coder you wants to develop POCO classes and then wire this up to talk to your database that is fine. OR , you can use the EDMX. There are variations of which you can use Model first, Migrations etc...

Code First is for both flexibility of whether you have an existing database or do not have a database.

I personally like to have many projects what to me replaced the "DAL" is a ProjectName.Data project look into the repository pattern ( and eventually the unit of work pattern).

You don't want to have business logic and code bleeding into your controllers. SRP (single responsibility principle) Keep your controller skinny. Nerddinner and a few other MVC projects on the internet try to make for a decent structure. MusicStore http://mvcmusicstore.codeplex.com/ and Contoso http://contosontiermvc.codeplex.com/ although they have a mvc3 main structure you will get a good insight as to how to structure your data.

  1. You can generate from database by trying to create the Entity Framework from an existing database. (I think you right-click on the surface of a new model and Generate from Database)

  2. They can reside in a different project, yes

Is the EDMX file still needs to be generated?

I may misunderstand your question, but if you have the database created, and you want to access it using EF Database First technique, then you will need to generate the entity framework model(EDMX) in order to use EF to access the database. But you will generate it from the database. There is a model first technique, which is what you do NOT want to do.

There is another option, instead of using database first, you can use code first, and instead of generating an EDMX, you can generate the fluent mappings. Essentially reverse engineering the code first model. This is the only way I know of to use EF to access the database without EDMX.

Where should the entity conceptual models reside in my application architecture? Can I have all my models (DB models, view models) in a separate project?

I usually have my EF models(entity models), whether they are EDMX or Code first, in a class library. This allows me to reuse that library across projects that might access the same database.

My ViewModels go in the Models folder in the MVC project, since they are specific to the Views in the MVC project.

Alot of people have another layer that is usually classes that have functions like GetPerson, GetOrder, GetOrders, etc. that allow you to call and they handle querying the entity model, or perhaps return IQueryable that allows your MVC project to then add additional criteria. This layer may or may not also populate ViewModel's from the query. There are lots of variations. You can read up on "repository patterns". I personally would strongly recommend you not implement this layer until you have used EF and MVC for awhile. That way you begin to see what is redundant, or clutters up your controller, and this will help you gauge better what variation of the repository pattern you would like to use. It's difficult to adopt a pattern properly if you haven't experienced the problems it solves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM