bloodfire1004
bloodfire1004

Reputation: 503

Can I use the generated model by Entity Framework directly?

I am pretty new to using Entity Framework. I am working on a Web MVC 2 project (for .NET 3.5) and am exploring using Entity Framework since I read that it is the recommended approach when working with data-access layer.

My question is, I have already created my database and used Visual Studio to create the Entity Models. Now, as should be, there are auto-generated classes for me to use. I've gone through some tutorials and they are using these directly.

However I am not sure. Going forward, I am thinking of needing some additional business logic to my models. So should I, as early as now, create my own Model classes?

Thank you in advance!

Upvotes: 3

Views: 1190

Answers (3)

miguelbgouveia
miguelbgouveia

Reputation: 2973

It depends, as always in the developing world.

If your project is very simple and you know that it will never grow in complexity, then it’s ok to not create a business logic layer.

Moreover, accessing directly your auto-generated classes in your controllers will turn your implementation much more faster.

If you don’t have these certainties then I advise you to create a business logic layer and use the auto-generated classes just as if they are data base tables that going to populate and persist your business logic models.

If you don't create a business logic layer you will suffer when maintaining and expanding your application.

Upvotes: 0

Anjani
Anjani

Reputation: 1038

I think you meant that you have generated models through designers.It doesn't matters whether you use Code First Approach (Creating classes first) or Database First approach. Models are representation of your tables and you can access models through Dbcontext where your models are represented as DbSets.You can use dbcontext for any kind of operations on models/tables based on logic implemented.

Business logics should be not be part of models but ideally two layers far from it. Business logic layer should call data access layer(DAL) and DAL should have logic to access/manipulate data through dbContext in EF.

See Repository pattern. (https://msdn.microsoft.com/en-us/library/ff649690.aspx)

This helps Business logic to be independent of underlying data source (whether sharepoint,sql server or a web service) because DAL layer has implementation to access data.This approach also avoids code repetition and helps in data centralization and is lesser prone to errors.Adding Separation of Concern also helps in better unit testing.

Upvotes: 4

Sadid Khan
Sadid Khan

Reputation: 1985

your data models are representing your database tables. models should not contain business logics. You can use other models to combine your dbmodel properties as you need.. apply your business logic in separate class. service, repository these concepts are used to make a project more manageable.. but u should not apply business logic in your model class.. it is not a good practice..

you can get some idea from here.. this may help you to understand.. https://softwareengineering.stackexchange.com/questions/213317/net-mvc-project-architecture-layering

Upvotes: 0

Related Questions