0

I'm new to the MVC way of developing applications and for the most part am enjoying. One thing I'm a bit confused about is the use of the Entity Framework. The EF usually (at least in my experience) defines multiple tables and relationships through the .edmx table. A couple of questions:

  • Why would I define a separate class file for a specific table if EF is building all of the classes that I need in the background?

  • From some of the validation approaches that I've seen, they want to define validation logic in the class related to a model for a table. If I'm using EF, will I have a .cs file describing the model and a .edmx describing that same table (in addition to its associated tables)?

  • If yes, how do you connect the .cs file to the .edmx definition so that CRUD flows easily from the EF?

Sorry if these seem like easy questions but I'm just trying to get my head wrapped around these fundamental concepts. Too many examples out there use only a single table where in my business, I NEVER write an application that uses a single table. There are always multiple tables in relation to each other with foreign keys. Thanks for your prompt responses.

2 Answers 2

2

For a tutorial that shows the use of partial classes -- in a Web Forms application but for MVC the same technique would be used -- see Adding Metadata to the Data Model in this tutorial:

http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-8

From your comment "The EF usually (at least in my experience) defines multiple tables and relationships through the .edmx table." it sounds like you are familiar only with Database First and Model First -- for an introduction to Code First and an explanation of the differences, followed by a series of tutorials with an MVC example using Code First, see this tutorial:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Sign up to request clarification or add additional context in comments.

Comments

0

Good questions, Darryl. Here are my responses to your bullet points:

  • Defining separate model classes that match the data models that EF creates is generally a good idea for the simple sake of separating your data access "stuff" from your business model objects that will get used throughout your app. Some people don't like this approach because it creates some amount of overhead when it comes to mapping your entities to POCOs but, if you use a tool such as AutoMapper, the overhead is minimal. The benefit lies in you creating a layer of separation between you and your (likely) evolving data model.

  • You could define validation logic in a buddy class (just a partial class that sits along-side your entity) but that would mean that you would be using that entity across your app and some would debate that that isn't the best idea. The alternative method, as mentioned above, is to create your own POCOs to mirror the entities that EF creates and place your validation attributes on the POCOs.

  • I mentioned this in the previous item but the way to do this would be to define buddy classes. Give EF buddy classes a Google and you should find plenty of examples on how to do that.

Just to add to all of this, if you choose to create POCO classes that mirror your EF entities, tools like AutoMapper can handle fairly complex relationships when it comes to mapping classes. So, if you have foreign key relationships in your data model, AutoMapper can understand that and map your POCO classes accordingly (i.e.: You have an entity that has a 1-to-many relationship and a POCO with a list of objects to mirror that relationship.)

I hope some of that helps...

3 Comments

Thanks so much Anthony. I really appreciate your insights and I will check out Automapper. I would suspect that having the buddy classes would allow me to put business logic in those classes which the Views could then call which I really desire to do seeing that I have a lot of repeating logic in my views (a definite no-no) that could be easily handled by methods in my variuos models. How does one link the buddy view to the EF for CRUD?
Well now, I think you're talking about two sides of the equation here... If you have repeating logic in your views, you'll want to look into making your own HTML helpers. You can take a look at Scott Gu's post here: Scott Gu's Site. As far as the entities go, you could put business logic in those buddy classes; however, I would recommend that you split your business logic into it's own layer within your application architecture.
Think of it this way... You call into a controller, your controller (by whatever means) will instantiate a "Business logic handler" object. This object would house any sort of repositories that would take care of data access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.