Coolthing Of Theday

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 9 April 2013

Two Firsts, one great taste... A "Database-First Code-First Entity Framework 5 Model"

Posted on 08:32 by Unknown

The Curly Brace - How to Create a Database-First Code-First Entity Framework 5 Model

Why would you ever want to have your EF5 model perform code-first operations, when using a database-first generated model? I can think of two reasons:

  1. Dynamically creating a disconnected SQL Server Compact Edition (CE) database on the client, with a high degree of security
  2. Silencing the developer who insists their database management tool has superior schema editing capabilities
In my case, I developed a read-only, disconnected client application that stored lots of data in a local SQL Server Compact Edition 4.0 database (CE), which was stored in their AppData\Local directory. Other requirements, which I will not discuss here, require the database files be created on-demand. We have the potential for new users that need a new CE file, on demand. The Database.Create() command of a code-first data model is ideal for this situation.
The average import was about 500MB, so not really something we want to pipe through the EF5 model. The most efficient way to periodically import data was through a BulkCopy command. CE has no BulkCopy command, but Erik created a library for that, here. Using this command requires the schema of both databases to match, which is satisfied by a database-first model.
The object of this exercise is to generate a database-first model from the schema defined in an existing database, and then turn that around to generate a new database from the EF5 backing store. Chocolate in your peanut butter, indeed.

...

Unfortunately, the code this message asks us to remove is generated by a T4 template. Let's get our hands into some code, and figure out how to remove the method.
This is a very easy fix. Famous last words, I know. But, this actually is an easy fix. Before we begin, here's the big, fat disclaimer:

  • This solution disables the safeguard Entity Framework 5 (EF5) puts in place, to prevent a database-first model from performing code-first model functions
  • This solution must be implemented every time you create a new, database-first EF5 model
  • Assume that updating the schema of the database by using code-first functions will cause data loss or corruption
  • No, I am not responsible for breaking your code, database, and/or data; that's all you

image

..."

Oh no... I think I'm a #2 (no wise cracks from the peanut gallery please...). I like the thought of Code First, but I've been doing Database First for so darn long that I'm having a problem breaking that mental block. But then again, having spent the week hand coding my entities and DAL... (Why? Because... um... Stored Procedures? um... err... because the magic of EF is kind of scary... um... err...  I'm lame and my brain is old? um... damn). sigh...

Anyway... While this seems like a cool kind of hack (hack in a good way), this might be something I'm going to have to look closer at.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in .Net, Development, EntityFramework, T4 | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Mr. 7,000! This is my 7,000th post...
    Before this post; After; 20 visits between taking these snaps? Oh wait, that's probably me searching for past related posts....
  • "Windows Server Essentials Media Pack" (DNLA Stream, HTML5 and Dashboard Media stuff)
    Microsoft Downloads - Windows Server Essentials Media Pack This pack enables the media streaming functionality for Windows Server 2012...
  • Rad Gate Post... Get your Red Gate Post here...
    simple talk - Melanie Townsend - Get a copy of the Red Gate Post We recently put together a newspaper of some of the best articles fr...
  • Windows Management Framework 4.0 (PowerShell 4, PowerShell ISE, Management OData, WMI, etc.) now available
    Keith Hill's Blog - PowerShell 4.0 Now Available You can get PowerShell 4.0 for down level operating systems now via the WMF 4.0 d...
  • Viasfora - Your new favorite Visual Studio Text/*ML Editing Extension?
    Winterdom - Introducing Viasfora A couple of days ago, I unveiled Viasfora , my latest attempt at building a decently packaged extensi...
  • "Windows Server [2012 R2]: The Best Infrastructure to Run Linux Workloads"
    In the Cloud - What’s New in 2012 R2: Enabling Open Source Software Part 4 of a 9-part series . ... There are a lot of great s...
  • [Hardware Review] Life with Haswell... Haswell/Harris Beach Intel SDS Ultrabook Review - Part 2
    "So Greg, how's life with Haswell been?" "Pretty Sweet! (Mostly)" I've been given an opportunity to review t...
  • Fuzzy Lookup Add-In for Excel (Insert lame "Fuzzy, wuzzy was an Excel..." snip here)
    Microsoft Downloads - Fuzzy Lookup Add-In for Excel The Fuzzy Lookup Add-In for Excel performs fuzzy matching of textual data in Exce...
  • Caliburn.Micro v1.5.0 released (CM gets Tasks, Async/Await and Share/Setting for RT... and bug fixes of course)
    Caliburn.Micro - Caliburn.Micro v1.5.0 "Release Notes This release fixes many bugs. It also adds support for Task and async/a...
  • Just about everything you ever wanted to know about SQL Server Date and Time Data Types...
    CodeProject - Date and Time Data Types and Functions - SQL Server (2000, 2005, 2008, 2008 R2, 2012) Introduction It would be bette...

Categories

  • .Net
  • 3DPrinting
  • AFeedYouShouldRead
  • Agile
  • ALM
  • Amazon
  • Amiga
  • Analytics
  • Android
  • ASP.NET
  • Azure
  • BigData
  • bing
  • Blogging
  • Book
  • BookReview
  • BUILD
  • C
  • C#
  • C++
  • Career
  • Cat
  • cheatsheet
  • ClickOnce
  • Cloud
  • ComputerHardware
  • css
  • Data
  • DBA
  • DependencyInjection
  • Deployment
  • Design
  • Development
  • devops
  • DVCS
  • ebook
  • EDD
  • Education
  • EnterpriseLibrary
  • EntityFramework
  • Exchange
  • Expression
  • gadget
  • Game
  • GIT
  • Google
  • Government
  • Hadoop
  • hardware
  • HardwareReview
  • HaswellReview
  • HTML5
  • Humor
  • IE
  • IEExtension
  • IfAllElseFails
  • IIS
  • ILMerge
  • Image
  • Infographic
  • interview
  • InversionOfControl
  • Java
  • Javascript
  • Kinect
  • LightSwitch
  • LINQ
  • Linux
  • LosAngeles
  • Lucene
  • Lync
  • MEF
  • Metro
  • MicrosoftOffice
  • MicrosoftOutlook
  • Mono
  • MVC
  • MVVM
  • NetMon
  • NLP
  • NoSQL
  • NuGet
  • OData
  • OneNote
  • OpenXML
  • Paint.Net
  • Personal
  • Photosynth
  • Physics
  • portable
  • Poster
  • PowerShell
  • Preparedness
  • Presentation
  • Prism
  • PrivateCloud
  • RegEx
  • RemoteDesktop
  • Reporting
  • RIAServices
  • Science
  • ScienceFiction
  • Scratch
  • Scrum
  • ServiceBus
  • SharePoint
  • Silverlight
  • SimiValley
  • SPA
  • Space
  • SQLServer
  • Storyboard
  • Surface
  • SVG
  • SystemAdministration
  • T4
  • TeamBuild
  • TeamFoundationServer
  • TechEd
  • Training
  • TypeScript
  • UnitTesting
  • UnityApplicationBlock
  • Utility
  • Veteran
  • VirtualMachine
  • Visio
  • VisualBasic
  • VisualStudio
  • WCF
  • Web X.X
  • Webcast
  • WebFeed
  • WebMatrix
  • Windows
  • Windows7
  • Windows8
  • Windows8.1
  • WindowsHomeServer
  • WindowsLiveWriter
  • WindowsPhone
  • WindowsServer
  • WinRT
  • WiX
  • WMI
  • WOPI
  • WPF
  • XAML
  • XBox360
  • XboxOne
  • zombie

Blog Archive

  • ▼  2013 (500)
    • ►  December (12)
    • ►  November (61)
    • ►  October (65)
    • ►  September (38)
    • ►  August (47)
    • ►  July (75)
    • ►  June (39)
    • ►  May (40)
    • ▼  April (42)
      • Unusual Microsoft Download of the Day: "Cancelling...
      • Enterprise Library 6 and Unity 3 are out today... ...
      • NuGet your Enterprise?
      • Infragistics Releases 13.1 with new set of Windows...
      • Getting into the mood... (well...) Microsoft Resea...
      • Virtual CD-ROM Control Panel download - A granddad...
      • Public Sector Developer Weblog highlights a whole ...
      • The First [12] Steps in using Caliburn Micro to bu...
      • You are such a Code Digger... Code Digger VS2012 e...
      • I trace, you trace, we all IntelliTrace (or want t...
      • Example using the Task Parallel Dataflow Library h...
      • Learning Log Parser Studio in two parts... (From I...
      • "The Essential Binary Repository Management Cheat ...
      • Bye bye Kona code name... Hello "Prism for Windows...
      • 14 Minute cartooned video guide to to building you...
      • Just about everything you ever wanted to know abou...
      • TFS/Visual Studio 2012 Update 3 CTP 1 is available...
      • A view into the creation of NASA'a spaceappschalle...
      • The final final definitive (for now) answer for Wi...
      • Taking Facial Detection to the next level (and ful...
      • DevOps & IntelliTrace, better than beer and pizza ...
      • [Humor] "How to make an awesome illustration [for ...
      • What do Fiddler, LinqPad, Excel and SharePoint hav...
      • What would the Xbox 360 “Achievement Unlocked” sou...
      • Unity vs. MEF - right for you, one is...
      • Learning, living, being SQL Server for Dev's video...
      • Going with the GeoFlow for Excel 2013... Free 3D v...
      • Office Web Apps Server isn't just for SharePoint.....
      • "The Last Visibility Converter" (you'll ever need)
      • Microsoft Script Explorer - So long and thanks for...
      • Two Firsts, one great taste... A "Database-First C...
      • Community AdventureWorks on Azure one year later, ...
      • Powering your Zipping with PowerShell and 7-Zip (a...
      • WPF is to PowerShell as the PowerShell ISE is to E...
      • Nothing like a little LinqPad fun for a Friday - "...
      • DLM on MSDN - Database Lifecycle Management curate...
      • Visual Studio 2012 Update 2 is RTW... (and it's a ...
      • Dev'ing for Ops - How to create System Center Exte...
      • Scrum Primer, the v2...
      • Now that's Qool
      • Sql Server info tip of the day: Alter Columns - Kn...
      • Open Source NFC Emulator for Windows Phone 8 with ...
    • ►  March (39)
    • ►  February (42)
Powered by Blogger.

About Me

Unknown
View my complete profile