Scott Hanselman

Entity Framework Magic Unicorn (and much more!) is now open source with take backs

July 19, '12 Comments [25] Posted in Data | Open Source
Sponsored By

The Unicorn is fineAlmost exactly two years from the day we announced Entity Framework "Magic Unicorn Edition" and just a few months after open sourcing of ASP.NET MVC 4, ASP.NET Web API, and Razor we're happy to announce we will release the source code for the Entity Framework under an open source license as well!

This is cool for a number of reasons. The EF team is continuing to move forward with a commitment to transparency. The community will be able to not only watch the check-ins and monitor the progress of the project, but also get involved themselves, submit pull requests for bug fixes, use daily builds and give feedback much more efficiently.

Just like the open source ASP.NET components and open source Azure SDKs, Entity Framework will continue to have the same full-time Microsoft engineers working on the project and will remain a fully supported Microsoft product that will ship both standalone AND with Visual Studio.

The code being open sourced includes the Entity Framework Runtime and NuGet packages, Code First, the DbContext API, the Entity Framework Power Tools as well as pieces that shipped originally with the .NET Framework. The team is also working to open source the EF Designer surface in the future as well.

By open sourcing EF we open up the design and development process that started the EF 4.1 preview builds. People who are interested can get nightly builds, see the source code changes, and engage in discussion about the design and implementation. The EF team is committed to opening up the development and involving the community and are going to work hard to make that happen.

Entity Framework will live in a Git repository at http://entityframework.codeplex.com and you're welcome to follow, fork, discuss and file bugs against it as you see fit. You could fork it just to fix a bug quickly to unblock work that uses EF, or you could create a custom version of EF with different capabilities. Since the changes can also be contributed back this gives people a lot greater flexibility in how they develop with EF.

Again, we've previously open sourced the Azure SDKs, the ASP.NET Web Stack, and now Entity Framework. These products all have the same teams assigned (and in most cases have more resources than before), the same developers coding and the same support guarantees. They are open source projects but they are also supported and released Microsoft products suitable for use in any company. They'll continue to ship with Visual Studio as well as standalone as packages in NuGet. The investment in all these technologies has been significant in the past year and investments will continue going forward with new versions and even more cool features.

Async - My Favorite New Entity Framework feature

Just as an example, I wanted to show a new feature in the upcoming EF6 that the team is hard at work on for release next year. Visual Studio 2012 introduces the Aync and Await concepts that make asynchronous code much easier to write (and understand). Damian, Levi and I talked about async and the surrounding concepts in this week's Hanselminutes podcast.

EF6 will introduce support for the new task-based asynchronous pattern when querying and saving data. You can actually look in the open source code base today and find the first parts of this feature are already checked in! ;)

As an example, using EF5 you could define a helper method that will find the store closest to a given location. Any code that calls this method would need to wait while it executed.

public Store FindClosestStore(DbGeography location)
{
using (var context = new StoreContext())
{
return (from s in context.Stores
orderby s.Location.Distance(location)
select s).First();
}
}

But in EF6 it is easy to turn this into an asynchronous method that can be left to execute while the calling code continues on with other tasks.

public async Task<Store> FindClosestStore(DbGeography location)
{
using (var context = new StoreContext())
{
return await (from s in context.Stores
orderby s.Location.Distance(location)
select s).FirstAsync();
}
}

This is just one feature but it's my favorite. You'll also soon see mapping of Stored Procedures as well as support for custom conventions using the Code First API.

Be sure to follow the Entity Framework Repository and subscribe to the check-ins via RSS to stay up on the latest features and improvements to EF! Also, do go chat directly with the Entity Framework team on Jabbr (powered by SignalR and Azure) in their room: http://jabbr.net/#/rooms/EntityFramework

Related Links


Sponsor: My personal thanks to DevExpress for sponsoring this week's feed! Check out their new stuff, it's amazing! DXperience 12.1 helps you realize your creative vision. Beautiful design and rich user experiences are at the center of the modern development conversation. From iPads to mobile phones, focus on great design. Try Now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Entity Framework Code First Migrations: Alpha - NuGet Package of the Week #10

July 28, '11 Comments [47] Posted in Data | NuGet | NuGetPOW
Sponsored By

Hot on the heels of my RFC blog post on product versioning, the Entity Framework team has released Entity Framework 4.1 Code First Migrations: August 2011 CTP. Cool. And it's July, too!

Or my preferred product name, "Migrating Magic Unicorns 0.5." It's probably best to think of this as 0.5 Alpha Migrations for EF but that's my guess at a name and not nearly as descriptive.

I showed early daily builds of EF Migrations at a few conferences recently, and encouraged folks to comment on the ADO.NET team blog. Now they've released bits for us to play with. This initial CTP is available via NuGet as the EntityFramework.SqlMigrations package.

EntityFramework.SqlMigrations - 0.5.10727.0

Here's the general idea. Be aware that this is NOT specific to the Web. You can do this in a Console App or whatever. I just like Hello World Web applications.

Short Walkthrough

Make a new ASP.NET MVC app. Go to Manage NuGet Packages from References and search for EntityFramework.SqlMigrations. Note the dependency it has. It'll upgrade your project's EntityFramework package as well.

Entity Framework SQL Migrations In NuGet

Now, make a new class with a simple model:

namespace MvcApplication15.Models
{
public class Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}

public class DemoContext : DbContext
{
public DbSet<Person> People { get; set; }
}
}

Scaffold out a quick Person Controller...

Add Controller

Visit it at /Person and make a few People.

 Index - Windows Internet Explorer (58)

The database that was created looks like this. There's a People table and the First column that is an nvarchar.

image

Now, let's add an Email field. I'll update the Person class to include Email:

public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}

Then, I'll compile and from the console inside Visual Studio, I'll do this:

PM> update-database
No pending custom scripts found.
Ensuring database matches current model.
- Performing automatic upgrade of database.
- Starting rebuilding table [dbo].[EdmMetadata]...
- Caution: Changing any part of an object name could break scripts and stored procedures.
- Starting rebuilding table [dbo].[People]...
- Caution: Changing any part of an object name could break scripts and stored procedures.
- Update complete.

I typed update-database, that's all. This is an automatic migration. See how the system compare the .NET type and the database and did what needed to be done:

image

Now, let's rename Email to EmailAddress. If I change the Person...

public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string EmailAddress { get; set; }
}

And type update-database...

PM> update-database
No pending custom scripts found.
Ensuring database matches current model.
- Performing automatic upgrade of database.
Update-Database : - .Net SqlClient Data Provider: Msg 50000, Level 16, State 127, Line 6 Rows were detected. The schema update is terminating because data loss might occur.
At line:1 char:16
+ Update-Database <<<<
+ CategoryInfo : NotSpecified: (:) [Update-Database], Exception
+ FullyQualifiedErrorId : UnhandledException,System.Data.Entity.Migrations.Commands.MigrateCommand

I'm told that data loss may occur. It can't tell that I want to rename that column or not. It doesn't know what it was before and what it wants to be. Maybe I want to drop Email and add EmailAddress? Who knows. Let me be explicit and give Migrations more context.

PM> Update-Database -Renames:"Person.Email=>Person.EmailAddress"
No pending custom scripts found.
Ensuring database matches current model.
- Performing automatic upgrade of database.
- The following operation was generated from a refactoring log file d5598498-a656-4ccd-1e93-bea562ab6e31
- Rename [dbo].[People].[Email] to EmailAddress
- Caution: Changing any part of an object name could break scripts and stored procedures.
- Update complete.

I'm not sure if I like that Renames: syntax. I'm sure the team would be interested in your opinion. But that works, as I can see in the database.

image

What Changes Can Migrations Detect Automatically?

From the ADO.NET blog:

Here is the full list of changes that migrations can take care of automatically:

  • Adding a property or class
    • Nullable columns will be assigned a value of null for any existing rows of data
    • Non-Nullable columns will be assigned the CLR default for the given data type for any existing rows of data
  • Renaming a property or class
    • See ‘Renaming Properties & Classes’ for the additional steps required here
  • Renaming an underlying column/table without renaming the property/class
    (Using data annotations or the fluent API)
    • Migrations can automatically detect these renames without additional input
  • Removing a property
    • See ‘Automatic Migrations with Data Loss’ section for more information

Moving to Staging/Production/etc

Once dev is correct, when you want to move to production, you would generate a script for your other database by doing a diff between what that DB looks like and what the code looks like.

For example, -script generates a script I can run myself with osql.exe or whatever.

update-database -Script -ConnectionString "SERVER=.\SQLEXPRESS;Database=PersonProd;Trusted_Connection=true;"

Which spits out something like (but more complex than) this:

CREATE TABLE [dbo].[People] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[EmailAddress] NVARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);

Call for Feedback

The EF Team has also this specific call for feedback when it comes to Custom Scripts:

Call for Feedback: From what we are seeing in our own internal use we don’t anticipate that custom scripts will be required very often. However, our efforts are somewhat sheltered from the ‘real world’ so we would love feedback on situations where you need to use custom scripts. In particular we are interested if there are significant scenarios where a code based alternative to writing raw SQL would be beneficial.

Leave your thoughts in my comments, or theirs and I'll make sure the right people get it.

Enjoy!

P.S. Note the EntityFramework.SqlMigrations NuGet package's exposed version number. It's 0.5.10727.0. ;)

Limitations

This is Alpha, so read the Limitations section. They are putting out rough things like this because they know we want to see bits earlier, but the trade off is limitations. Here's a few. Read the list for the rest.

  • There is no provider model, this release only targets SQL Server, including SQL Azure. SQL Compact and other providers are not supported. We are currently working through what the provider model should look like for migrations.
    • Question: What do you think? How important is this?
  • Migrations currently needs to run in full trust. This isn’t an issue when working inside of Visual Studio but if consuming the migrations assembly from custom code you may want to run in medium trust. We are looking at ways to support this in a later release.
  • This release is only available via NuGet. As we support more scenarios such as team build and an ‘outside of Visual Studio’ command line experience we will also support more installation options.
    • Scott: Limitation? That's lovely! ;)
  • Downgrade is currently not supported. When generating custom scripts you will notice that the script is named ‘Up.sql’ but there is no corresponding ‘Down.sql’. We are planning to add downgrade functionality prior to RTM but it is not available in this release.

I'm glad we're getting migrations. It's been a missing LEGO piece for a while.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Hanselminutes Podcast 262 - The Rise of the Micro-ORM with Sam Saffron and Rob Conery

April 21, '11 Comments [11] Posted in Data | Open Source | Podcast
Sponsored By

Scott's at Mix this week and he sits down with Sam Saffron and Rob Conery to talk about their Micro-ORMs. What have they done in less than 400 lines of code, that the rest of the planet needs a dozen assemblies for? Should you abandon your ORM and start writing inline SQL? All this and more.

Here's a few examples to look at while you listen to the show:

Dapper

var guid = Guid.NewGuid();
var dog = connection.ExecuteMapperQuery<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });

Massive

var tbl = new Products();
var products = tbl.All(where: "CategoryID = @0 AND UnitPrice > @1", orderBy: "ProductName", limit: 20, args: 5,20);

Download: MP3 Full Show

Links:

NOTE: If you want to download our complete archives as a feed - that's all 262 shows, subscribe to the Complete MP3 Feed here.

Also, please do take a moment and review the show on iTunes.

Subscribe: Subscribe to Hanselminutes or Subscribe to my Podcast in iTunes or Zune

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface and developer tools, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET AJAX,MVC,Silverlight, Windows Forms and WPF. Enjoy developer tools like .NET Reporting,ORM,Automated Testing Tools, Agile Project Management Tools, and Content Management Solution. And now you can increase your productivity with JustCode, Telerik’s new productivity tool for code analysis and refactoring. Visitwww.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Hanselminutes Podcast 258 - Inside Entity Framework 4.1 CodeFirst with Jeff Derstadt and Tim Laverty

March 30, '11 Comments [6] Posted in Data | Podcast
Sponsored By

imageThis week Scott sits down with the Lead Dev and Lead PM for Entity Framework 4.1 (Magic Unicorn Edition!) to talk about the improvements from the first version. What's improved and changed? What do they think about NHibernate or just doing SQL on your own?

Also, check out the video interview with Jeff Derstadt on Hanselminutes on 9!

Download: MP3 Full Show

Links:

Plus, you can get Entity Framework from NuGet and use it in your project immediately.

image

NOTE: If you want to download our complete archives as a feed - that's all 258 shows, subscribe to the Complete MP3 Feed here.

Also, please do take a moment and review the show on iTunes.

Subscribe: Subscribe to Hanselminutes or Subscribe to my Podcast in iTunes or Zune

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface and developer tools, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET AJAX, MVC, Silverlight, Windows Forms and WPF. Enjoy developer tools like .NET Reporting,ORM,Automated Testing Tools, Agile Project Management Tools, and Content Management Solution. And now you can increase your productivity with JustCode, Telerik’s new productivity tool for code analysis and refactoring. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web

Hanselminutes Podcast 225 - Learning about NHibernate 3 with Jason Dentler

August 12, '10 Comments [4] Posted in Data | Open Source | Podcast
Sponsored By

NHLogoSmall My two-hundred-and-twenty-fifth podcast is up. Scott chats with Jason Dentler about NHibernate and their new 3.0 release. Jason is the author of the upcoming "NHibernate 3 Cookbook" from Packt Publishing. Is NHibernate hard and scary? Jason gets Scott up to speed and talks open source community.

NOTE: If you want to download our complete archives as a feed - that's all 225 shows,subscribe to the Complete MP3 Feed here.

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Download: MP3 Full Show

Links from the Show

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show.

Hansel_Promo

Hanselminutes podcasts listeners can get $50 off any Telerik product this summer. All interested listeners should drop an email to podcast@telerik.com and mention the Hanselminutes promo and their sales team will reply with the special $50-off coupon code.

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface and developer tools, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET AJAX,MVC,Silverlight,Windows Formsand WPF. Enjoy developer tools like .NET reporting,ORM,Automated Testing Tools, TFS, and Content Management Solution. And now you can increase your productivity with JustCode, Telerik’s new productivity tool for code analysis and refactoring. Visitwww.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Page 1 of 2 in the Data category Next Page

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.