Can I directly use the generated Entity Framework model?

I am new to using Entity Framework. I am working on a Web MVC 2 project (for .NET 3.5) and am learning with Entity Framework as I read that this is the recommended approach when dealing with the data access layer.

My question: I already created my database and used Visual Studio to create Entity models. Now, as it should be, there are auto-generated classes for me. I went through some tutorials and they use them directly.

However, I'm not sure. Going forward, I am thinking about the need for additional business logic for my models. So should I be creating my own model classes now?

Thank you in advance!

+3


source to share


3 answers


I think you meant that you created the models through the designers. It doesn't matter if you use Code first (creating classes first) or Database First. Models are representations of your tables, and you can access models through Dbcontext, where your models are represented as DbSets. You can use dbcontext for any operations on models / tables based on the implemented logic.

Business logic should not be part of the models, but ideally two levels are far from it. The business logic layer should call the data access layer (DAL), and the DAL should have logic to access / process data via dbContext in EF.



See the repository template. ( https://msdn.microsoft.com/en-us/library/ff649690.aspx )

This helps the business logic to be independent of the underlying data source (be it sharepoint, sql server or web service) since the DAL has an implementation for data access. This approach also avoids code repetition and helps in centralizing data and is less prone to errors. Adding Separation of Concern also helps in more efficient unit testing.

+3


source


your data models represent your database tables. models should not contain business logic. You can use other models to combine your dbmodel properties as needed. Use your business logic in a separate class. service, repository, these concepts are used to make the project more manageable .. but you shouldn't apply business logic in your model class .. this is not a good practice.



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

+1


source


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 okay not to create a business logic layer.

Also, accessing your auto-generated classes in your controllers will speed up your implementation significantly.

If you do not have these definitions, I advise you to create a business logic layer and use the automatically generated classes, just as if they were database tables that are going to populate and store your business logic models.

Unless you are creating a business logic layer that will suffer as you maintain and extend your application.

0


source







All Articles