Top Banner
C# Entity Frameworks 101 Sept 06, 2014 By Rich Helton
29
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Entity frameworks101

C# Entity Frameworks101

Sept 06, 2014By Rich Helton

Page 2: Entity frameworks101

What are Entity Frameworks(EF)?

•A Microsoft Object-Oriented Mapper (ORM) that generates the mapping of objects to SQL databases in Visual Studio.

Page 3: Entity frameworks101

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.

Page 4: Entity frameworks101

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.

Page 5: Entity frameworks101

MVC Components

Page 6: Entity frameworks101

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.

Page 7: Entity frameworks101

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.

Page 8: Entity frameworks101

EF Creation

Page 9: Entity frameworks101

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.

Page 10: Entity frameworks101

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.

Page 11: Entity frameworks101

DBContext Code

Here is an example of defining the DB structure through code.

Page 12: Entity frameworks101

Program Code

Example code of creating the DB, tables and adding fields.

Page 13: Entity frameworks101

Created the DB

Page 14: Entity frameworks101

Creating an EFModel

Page 15: Entity frameworks101

Creating a Project

Page 16: Entity frameworks101

Add a Data Model

Page 17: Entity frameworks101

Generate from DB

Page 18: Entity frameworks101

Set the Entity Name

Page 19: Entity frameworks101

We can visually view the models

Page 20: Entity frameworks101

We can delete the tables, not DB

Page 21: Entity frameworks101

Let's generate the tables from the models

Page 22: Entity frameworks101

Visual Studio builds the SQL for the table

Page 23: Entity frameworks101

Let's run the SQL from Visual Studio

Page 24: Entity frameworks101

The tables were built from the EF models

Page 25: Entity frameworks101

Populating the tables

Page 26: Entity frameworks101

The models have been previously created

Page 27: Entity frameworks101

Populating

Instead of DBContext, the generic context, we use the specific DB context that we defined when creating models, called MyNewTableEntities.

Page 28: Entity frameworks101

Let's add a row to the Address table

Page 29: Entity frameworks101

Done....