Entity Framework Code First Migrations

Slides:



Advertisements
Similar presentations
Getting Started with Oracle and .NET
Advertisements

Custom REST services and jQuery AJAX Building your own custom REST services and consuming them with jQuery AJAX.
Final Presentation WINTER 2009 – SUMMER 2009 PRESENTED BY: George Kour Hany Danial SUPERVISOR: Victor Kulikov Networked Software Systems Laboratory DEPARTMENT.
.NET 3.5 SP1 New features Enhancements Visual Studio 2008 SP1 New features Enhancements Additional features/enhancements.
MSDN Connection Get personalised information on the topics and technologies you want Profile yourself today and get updates via RSS Get personalised information.

Object Relational Mapping – ORM Entity Framework
Small Business Server and Windows SharePoint Services David Overton
Software Testing with Visual Studio 2013 & Team Foundation Server 2013 Benjamin Day.
The SSDT way or the highway
Installation and Deployment in Microsoft Dynamics CRM 4.0
Building Enterprise Applications Using Visual Studio ®.NET Enterprise Architect.
Part 05 – Code First Migrations and Azure Deployment Entity Framework and MVC Series Tom Perkins NTPCUG.
BizTalk Deployment using Visual Studio Release Management
Becky Bertram SharePoint MVP
René Balzano Technology Solution Professional Data Platform Microsoft Switzerland Database Development with SQL Server Data Tools (SSDT)
Query Processing in Mobile Databases
DYNAMICS CRM AS AN xRM DEVELOPMENT PLATFORM Jim Novak Solution Architect Celedon Partners, LLC
DotNetNuke v4 on ASP.NET v2 Stan Schultes Stan Schultes Enterprise architect / application developer Enterprise architect / application developer Conference.
Entity Framework Code First End to End
BIT 286: Web Applications Lecture 04 : Thursday, January 15, 2015 ASP.Net MVC - Models.
Intro to Entity Framework By Shahed Chowdhuri Don’t drown in database design during WakeUpAndCode.com.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
OVERVIEW ON HOW ENTITY FRAMEWORK CODE FIRST CAN EASE THE DEVELOPMENT PROCESS Entity Framework Code First 11/19/2013 Joe Walling Copyright Walling Info.
Tips and Tricks for Managing and Administering your Enterprise Project Management Server Solution Mike Joe / Karthik Chermakani Software Test Engineer.
Partners’ Webinar 01/31/2013 Karol Jarkovsky Solution Architect Upgrading Kentico.
Sage CRM Developers Course Using the Component Manager.
Entity Framework: Code First SoftUni Team Technical Trainers Software University
DEV303 ASP.NET: Leveraging Visual Studio.NET For Web Projects Tony Goodhew Product Manager Microsoft Corp.
Anand Maheshwari Product Highlights GA iWay Data Quality Portal 1.
Mirek Sztajno SQL Server Security PM
Data Access Layer Shahed Chowdhuri Using Code-First Migrations.
Database Projects in Visual Studio Improving Reliability & Productivity.
Adxstudio Portals Training
Entity Framework 7 Who Are You & What Have You Done to my ORM?
Simplifying the Code First Approach in the Entity Framework Dhananjay Kumar Infragistics Consultant Microsoft MVP
Entity Framework 7: What’s New? Ricardo Peres Technical Evangelist at Simplifydigital. Microsoft
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Integrating and Extending Workflow 8 AA301 Carl Sykes Ed Heaney.
Developing SQL/Server database in Visual Studio Introducing SQL /Server Data Tools Peter Lu.Net Practices Director Principle Architect Nexient March 19.
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core RC2 * aka ASP.NET 5 before.
ASP.NET Core* 1.0 The Future of Web Apps Shahed Chowdhuri
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com A Quick Overview of ASP.NET Core * aka ASP.NET 5 before.
Sitecore upgrades The Past, The Present, The Future.
ASP.NET Core* Shahed Chowdhuri Sr. Technical WakeUpAndCode.com Deploying Your Web Apps * aka ASP.NET 5 before RC1.
PowerApps and Microsoft Flow for SharePoint Developers Brian T. Jackett Sr. Premier Field Engineer, Microsoft.
ASP.NET Core* in 2017 The Future of Web Apps Shahed Chowdhuri
Xamarin Development with
DevOps with ASP.NET Core and Entity Framework Core
Building Enterprise Applications Using Visual Studio®
Stress Free Deployments with Octopus Deploy
Introduction to .NET Florin Olariu
EF Code First (Advanced)
Entity Framework: Code First
Data centric apps for web, desktop and mobile with EF5
ASP.NET Core* 1.0 The Future of Web Apps Shahed Chowdhuri
Entity Framework Core*
SQL Server Data Tools Gert Drapers
An Introduction to Entity Framework
Intro to Unit Testing with tSQLt
A Quick Overview of ASP.NET Core 1.0
Microsoft Visual Studio 2010 for Web Deployment
Presented by : Chirag Dani & Dhaval Shah
SSDT and Database Project Basics
Entity Framework Code-First Migrations
Power BI.
TechEd /3/ :48 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Implementing Entity Framework with MVC Jump Start
ASP.NET Core 2.0 The Future of Web Apps Shahed Chowdhuri
Presentation transcript:

Entity Framework Code First Migrations Don’t drown in database design during development By Shahed Chowdhuri Entity Framework Code First Migrations (title page) Don’t drown in database design during development WakeUpAndCode.com @shahedC

Thanks to our Sponsors Sponsors

Tech Check Are you familiar with…? EF Code-First Migrations ASP .NET Entity Framework EF Code-First Development ASP .NET MVC Are you familiar with: ASP.NET MVC? Entity Framework? Code First development? Code First Migrations? (slow-reveal diagram + ask the audience)

Introduction Enterprise Web Application ASP.NET MVC web app DB We are building an enterprise web application with ASP.NET MVC, with a SQL Server back end? But how exactly will we do it?

Options and Alternatives A. Database-First (EDMX) B. Model-First (Visual Designer) C. Code-First (automatic migrations) D. Code-First (manual migrations) Options and Alternatives Database-First (EDMX) Model-First (visual designer) Code-First with automatic migrations Code-First with manual migrations

A. Database-First (EDMX) ASP.NET MVC app code DB updates EDMX Model DB ADO.NET Entity Data Model (EDMX) Code uses EDMX A. Options and Alternatives Database-First (EDMX)

A1. Generate EDMX A1. Options and Alternatives: Database-First (EDMX) Right-click project, click Add -> ADO.NET Entity Data Model, then select existing DB.

A2. Update Model From Database A2. Options and Alternatives: Database-First (EDMX) Update your database table(s). Open the EDMX designer, right-click within it, select Update Model from Database.

B. Model-First (Visual Designer) B. Options and Alternatives Model-First (visual designer) Source: MSDN

C.Code-First (automatic migrations) ASP.NET MVC app code uses .NET models (hand-coded) generates auto- updates Migration code (auto-generated) C. Options and Alternatives Code-First with automatic migrations DB

D. Code-First (manual migrations) ASP.NET MVC app code uses .NET models (hand-coded) generates manually updates Migration code (auto-generated) D. Options and Alternatives Code-First with manual migrations DB

What are we trying to solve? Avoid tug-of-war between : Developers on your team Developers on other teams Database Architects Database Administrators

Conflicts Within Organization DB Architects Dev Team X Dev Team Y Your Dev Team DB Admins Avoid tug-of-war between : Developers on your team Developers on other teams Database Architects Database Administrators

5. Push Code + Migrate Server DB Main Agenda 1. Models & Mapping 2. Connection Strings 3. Enable Migrations 4. Update Database 5. Push Code + Migrate Server DB 6. Process Workflow Agenda Add models and mapping in code Connection String Enable Migrations Update Database Push code + Migrate server DB Process Workflow

1. Models & Mapping 1. Models & Mapping

2. Connection Strings For more info: Web.config configSource Machine config For more info: http://wakeupandcode.com/all-your-database-are-belong-to-us/

2a. Connection Strings (explained) How can I point to my own DB? Each dev has own ConnectionStrings.config How do I avoid checking in to Source Control? Remove file from .csproj, keep it local How will server deployments work? Use XML Transforms, i.e. Web.Prod.Config Do I have to include credentials in .config? No, use machine access to SQL server instead 2. Connection Strings (explained) How can I point to my own DB? How do I avoid checking in to Source Control? How will server deployments work? Do I have to include credentials in .config? For more info: http://wakeupandcode.com/all-your-database-are-belong-to-us/

3. Enable Migrations 3. Enable Migrations Enable Migrations Configuration.cs Add Seed data

4. Update Database model code migration code migration code + seed Run Seed Data Verify SQL Verify DB migration code + seed (sql) dev DB

5. Push Code + Migrate Server DB pull code CI 5. Push Code + Migrate Server DB Push code to source control Other developers pull and Update Database Migrate server DB via migrate.exe Use TeamCity to automate this Server DB migrate.exe

6. Process Workflow DB DB architects dev teams communication Handle existing database Comply with enterprise requirements

Code Walkthrough Code Walkthrough (First, Enable-Migrations) Update Model with new field Add-Migration Update-Database Verify in DB

Making Migrations Work 1. Discuss upcoming changes with teams. 2. Inspect/Update migration code 3. Update your DB before check-in. (use -script flag to inspect SQL first) 4. Run migrate.exe after deployments. (see \packages\EntityFramework.5.0.0\tools\) DB Making Migrations Work 1. Discuss upcoming changes with teams. 2. Inspect/Update migration code 3. Update your DB before check-in. (use -script flag to inspect SQL first) 4. Run migrate.exe after deployments. (see \packages\EntityFramework.5.0.0\tools\)

Upgrade or Downgrade Rollback to a specific migration Rollback all migrations (version 0) Rollback all migrations ($InitialDatabase) Upgrade or downgrade * Rollback to a specific migration * Rollback all migrations (version 0) * Rollback all migrations ($InitialDatabase)

Migration History Stored in table __MigrationHistory Metadata for your EF Migrations 1 new record for every migration Automatically generated Used for upgrades & downgrades Upgrade or downgrade * Rollback to a specific migration * Rollback all migrations (version 0) * Rollback all migrations ($InitialDatabase)

Common Issues Pushing model changes without adding a migration. Others will see a Pending Migration! Using an existing database Use empty Migration if Model added after Table Customizing constraint names Manually edit your Migration code DB Common Issues 1. Pushing model changes without adding a migration. - Others will see a Pending Migration! 2. Using an existing database - Use an Empty Migration! 3. Customizing constraint names - Manually edit your Migration code

Additional Topics Additional Topics Relationships Attributes Additional Topics Relationships Attributes (Data-Annotation Attributes)

Relationships DBContext .OnModelCreating() Related objects Customizing constraint names Source: Excella Lean presentation https://github.com/excellaco/ExcellaLean

Data-Annotation Attributes Source: http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx Source: MSDN

Conclusion Continuous Development Code First Migrations allows: Dev Team Synchronized Server Deployment Database Versioning Continuous Development Code First Migrations allows: Continuous development Synchronization among team members Database versioning Server deployment (diagram with star pattern)

Downloads http://wakeupandcode.com/downloads/ Downloads

Troubleshooting the Sample Trouble with Package Manager commands? Try the following: Delete your Packages folder first Open .sln in VS2012 Restore NuGet Packages when prompted Clean Solution Uninstall EF via NuGet Reinstall EF via NuGet Rebuild your solution Troubleshooting the Sample Clean Solution Remove/Restore Packages Uninstall/Reinstall EF via NuGet

Online Resources EF Code First Migrations on MSDN: http://msdn.microsoft.com/en-US/data/jj591621 Web.config & configSource: http://wakeupandcode.com/all-your-database-are-belong-to-us/ PluralSight EF Migrations video tutorial: http://pluralsight.com/training/Courses/TableOfContents/efmigrations My blog post on EF Code First Migrations: http://wakeupandcode.com/entity-framework-code-first-migrations/ Code First Data Annotations (Julie Lerman) http://msdn.microsoft.com/en-us/data/jj591583.aspx Data Annotations in the Entity Framework (MSDN) http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx Troubleshooting http://stackoverflow.com/questions/10999561/exception-raised-when-im-trying-enable-migrations-in-ef-4-3-1 Online Resources

Questions? http://WakeUpAndCode.com @shahedC Questions?