You are on page 1of 24

MySQL and the Entity Framework

Reggie Burnett Sun Microsystems

About MySQL
• 14 years of development • Acquired by Sun in Feb 2008 • 12 million product installations • 85,000 downloads a day • Performance, reliability, scalability • Easy to use, administer, maintain

Leadership, innovation, market acceptance


0 Powered by MySQL 3 . SaaS.Industry-Leading Customers Web / Web 2. Hosting Telecommunications Enterprise 2.0 OEM / ISV's On Demand. 4 . Consider it 100 •reggie. •A peek under the hood at how Connector/Net works with the entity framework •A demonstration using MySQL and the entity framework •A quick look at some of the work arounds we use to work with MySQL •How you can reach me? • •www.What will we cover today? •A brief overview of the entity framework •This will not be an exhaustive review.

What is the Entity Framework? Released in July as part of Visual Studio 2008 SP1 Entities You want to code against objects that make sense for your application but you want to work against any data source. 5 .

What can an entity represent? … almost anything! It SQL Data Services (SDS)) Google BigTable 6 .

Net? •Normal ORM arguments here •Change tracking. use of conceptual objects •Application portability instead of LINQ to SQL? •Entity Framework has a full provider model allowing future use of all types of data stores •Entity Framework is much more flexible than L2S (inheritance and complex types) 7 .So why use the Entity Framework instead of ADO.

)? “So the differentiator is not that the EF supports more flexible mapping than nHibernate or something like that.So why use the Entity Framework instead of Nhibernate (or …. it's that the EF is not just an ORM--it's the first step in a much larger vision of an entity-aware data platform -.e.Danny Simmons (Entity Framework team) •Fits better into the long term data platform vision from Microsoft •Many future Microsoft tools will be leveraging the entity framework including tools such as SQL Reporting Services •Allows common tools to be written (i. EF model browser in Visual Studio) 8 .

I need that mapping! Conceptual (CSDL) Mapping (MSL) Storage (SSDL) 9 .Scotty.

The man behind the curtain Entity Framework converts query into queries against physical store …which are then converted into command trees INSERT … Connector/ Net … which are converted into SQL by Connector/Net 10 .

dll) Connector/net (mysql.entity.How the components stack up We use a separate assembly for compatibility reasons Your Application EF ( MySQL 11 .data.

data.config (installutil mysql.and registered in on a local box • Web deployment steps will work locally as well • Installer will place components where they need to be – You must use the installer to get design time modeling • If not using the installer… – – – – Mysql...dll) but does not need to be installed using installutil.dll should be installed into GAC .dll should be installed into the GAC . 12 .

MySqlClient" description=".data.MySqlClient.0.0.Net Framework Data Provider for MySQL" type="> 13 .data.Deployment to the web • Same procedure as deploying mysql. and mysql. Version=6.Data. MySql. PublicKeyToken=c5687fc88969c44d" / </DbProviderFactories> </system.dll • Modify your web.config so the provider can be found: <system.Data.entity.5.Data.MySqlClientFactory.dll to the web • Create a reference from your project to> <DbProviderFactories> <clear/> <add name="MySQL Data Provider" invariant="MySql.

object IServiceProvider.GetService(Type serviceType) { // use reflection to return implementation of DbProviderServices } …. Provider discovery 2. SQL Generation Provider Discovery public sealed class MySqlClientFactory : DbProviderFactory.A peek under the hood Our provider is broken down into 3 areas. 1. Manifest and store specification 3. IServiceProvider { …. } 14 .

Manifest and store discovery ProviderManifest.msl Provides a mapping between store objects and entity framework objects <cs:EntityContainerMapping …> <cs:EntitySetMapping Name="Tables" StoreEntitySet="STables" TypeName="Store.ssdl Provides metadata queries and store definition <EntitySet Name="STables" EntityType="Self.Table"> <DefiningQuery> SELECT … </DefiningQuery> </EntitySet> SchemaMapping.Table"> <cs:ScalarProperty Name="Id" ColumnName="Id" /> </cs:EntitySetMapping> 15 .xml Lists store specific data types and functions <Type Name="time" PrimitiveTypeKind="Binary"> <FacetDescriptions> <MaxLength DefaultValue="8" Constant="true" /> <FixedLength DefaultValue="true" Constant="true" /> </FacetDescriptions> </Type> SchemaDefinition.

SQL Generation • The elephant in the room • Generates code for 4 different types of command trees – – – – DbQueryCommandTree DbInsertCommandTree DbUpdateCommandTree DbDeleteCommandTree • Can generate very long and complex SQL queries • Generated SQL is being simplified in EF 4 16 .

DEMO Creating a simple entity framework application. 17 .

Handling transactions • TransactionScope is the easiest – Know the current limitations of this • Object Services supports transaction scopes • EntityConnection supports the BeginTransaction method – Returns an EntityTransaction object – EntitySQL doesn’t support DML • Remember to use transaction enabled storage engines like InnoDB • DEMO – Inserting records 18 .

Inheritance is a good thing Two types are currently supported Table per type • One to one mappings • Can use a base entity • Also table per concrete type Table per hierarchy • Uses single table • Discriminator • Top entity usually abstract 19 .

DEMO Simple Inheritance 20 .

4 natively supports in/out and out parameters • Best practice is to make your schema look as much like SQL Server as possible! 21 .Unsigned Types and Stored Procs • They are supported – Not directly supported by the entity framework – We use the next larger integer type to represent an unsigned column • Support for stored procedures – They are supported – They work in the wizard – MySQL 5.

1 will not include native LinQ support – We had originally planned this but decided on other priorities – Targeting 6. LinQ and other MS data-centric initiatives.0 • Connector/Net 6.3 for some form of LinQ support – Very interested in enabling LinQ on Mono! 22 .0 is out now! – Has support for Entity Framework 1.Planned support • We are committed to supporting Entity Framework. • Connector/Net 6.

jsp?spid=f17eb968-ce40-48c5-9f63-5deb8e339216 Save on a Sun server/storage > http://www. > Free trial offer — MySQL Enterprise. > http://www. > http://www.sun.sun.What We Suggest: Learn about the most popular open-source database. then save.xml?intcmp=1390 23 .com/mysql/trial Take advantage of the Enterprise Unlimited Server Try before you buy. > •Chance to Win A $150 Amazon.html >“For ISVs: Architecting Embedded Databases for High Performance Applications”.1” Tues 15 Sept. 10 am PT –http://mysql.html •MySQL ISV / OEM Customers –http://mysql.html?s=oem 24 .com/news-and-events/on-demand-webinars/ Resources •Upcoming Webinars >“For ISVs: What's New in MySQL Connector/NET Wed 23 •Contact MySQL – Gift Certificate! >Just complete MySQL's 3 minute survey on your product's development cycle –http://www. 10 am PT –http://mysql.•Webinar replay (posted in three days) –http://mysql.