Date post: | 14-Jun-2015 |
Category: |
Software |
Upload: | rich-helton |
View: | 144 times |
Download: | 0 times |
C# Entity Frameworks101
Sept 06, 2014By Rich Helton
What are Entity Frameworks(EF)?
•A Microsoft Object-Oriented Mapper (ORM) that generates the mapping of objects to SQL databases in Visual Studio.
Benefits of EF
Reduced Development time by generation of code and diagrams.
Integration into SQL Server and other Microsoft Tools and Frameworks, including Visual Studio.
Dealing with business objects instead of SQL code, better Object design.
Reduced security risks by not having dangling SQL scripts.
ASP.NET MVC
Is Microsoft's framework in support of the Model-View-Controller, the most popular design pattern in the world for rapid development.Entity Frameworks are used heavily with ASP.NET MVC, but they are a completely seperate framework that are used independently as well.
MVC Components
ASP.NET MVC benefits
Enables Test-Driven Development (TDD). Provides rapid development for developing
ASP.NET in Visual Studio. Supports IIS backend code for enhanced
functionality. Provides clean separation of concerns(SoC)
between different components.
What is TDD?
Test-Driven Development (TDD) is a software development process that relies on the repetition of a very short development cycle by utilizing automated test cases that defines new functionality and programming to the passing these tests.
EF Creation
DB Creation
We can create a DB from code,called code-first, including a table and populate the table will a fields.
By default, it will default to the local instance of .\SQLExpress.
We can also create tables and add fields from a Entity model as well, which is the mapping of the Entity objects.
Code-First DBContext
The recommended way to work with context is to define a class that derives from DbContext.
Use the DbSet properties that represent collections of the specified entities in the context.
DBContext Code
Here is an example of defining the DB structure through code.
Program Code
Example code of creating the DB, tables and adding fields.
Created the DB
Creating an EFModel
Creating a Project
Add a Data Model
Generate from DB
Set the Entity Name
We can visually view the models
We can delete the tables, not DB
Let's generate the tables from the models
Visual Studio builds the SQL for the table
Let's run the SQL from Visual Studio
The tables were built from the EF models
Populating the tables
The models have been previously created
Populating
Instead of DBContext, the generic context, we use the specific DB context that we defined when creating models, called MyNewTableEntities.
Let's add a row to the Address table
Done....