mysql entity framework

Post on 26-Mar-2015

196 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Sun Microsystems

Reggie Burnett

Sun Microsystems

MySQL and the Entity Framework

2

About MySQL

Leadership, innovation, market acceptance

• 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

3

Industry-Leading Customers

Powered by MySQL

Enterprise 2.0TelecommunicationsOn Demand, SaaS, Hosting

Web / Web 2.0 OEM / ISV's

4

What will we cover today?

•A brief overview of the entity framework•This will not be an exhaustive review. Consider it 100 level.

•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.twitter.com/rykr

•www.reggieburnett.com

•reggie.burnett@sun.com

5

What is the Entity Framework?

You want to code against objects

that make sense for your application

but you want to work against any data source.

Entities

Released in July as part of Visual Studio 2008 SP1

6

What can an entity represent?

It … almost anything!

SQL Data Services (SDS))

Google BigTable

7

So why use the Entity Framework

•Normal ORM arguments here

•Change tracking, use of conceptual objects

•Application portability

instead of ADO.Net?

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)

8

So why use the Entity Framework

•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.e. EF model browser in Visual Studio)

instead of Nhibernate (or ….)?

“So the differentiator is not that the EF supports more flexible

mapping than nHibernate or something like that, 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

-- Danny Simmons (Entity Framework team)

9

Scotty, I need that mapping!

Conceptual(CSDL)

Storage(SSDL)

Mapping(MSL)

10

The man behind the curtain

Entity Framework converts

query into queries against

physical store

…which are then converted

into command trees

… which are converted into SQL by Connector/Net

Connector/Net

INSERT …

11

How the components stack up

MySQL

Connector/net (mysql.data.dll)

EF (mysql.data.entity.dll)

Your Application

We use a separate assembly for compatibility reasons

12

Deployment 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.data.dll should be installed into GAC

– ..and registered in machine.config (installutil mysql.data.dll)

– Mysql.data.entity.dll should be installed into the GAC

– .. but does not need to be installed using installutil.

13

Deployment to the web

• Same procedure as deploying mysql.data.dll to the web

• Create a reference from your project to mysql.data.dll and mysql.data.entity.dll

• Modify your web.config so the provider can be found:

<system.data>

<DbProviderFactories>

<clear/>

<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"

description=".Net Framework Data Provider for MySQL"

type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,

Version=6.0.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /

</DbProviderFactories>

</system.data>

14

A peek under the hood

Our provider is broken down into 3 areas.

1. Provider discovery

2. Manifest and store specification

3. SQL Generation

Provider Discovery

public sealed class MySqlClientFactory : DbProviderFactory, IServiceProvider

{

….

object IServiceProvider.GetService(Type serviceType)

{

// use reflection to return implementation of DbProviderServices

}

….

}

15

Manifest and store discovery

ProviderManifest.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.ssdl Provides metadata queries and store definition

<EntitySet Name="STables" EntityType="Self.Table">

<DefiningQuery> SELECT … </DefiningQuery></EntitySet>

SchemaMapping.msl Provides a mapping between store objects and entity

framework objects<cs:EntityContainerMapping …>

<cs:EntitySetMapping Name="Tables" StoreEntitySet="STables" TypeName="Store.Table">

<cs:ScalarProperty Name="Id" ColumnName="Id" /></cs:EntitySetMapping>

16

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

17

DEMOCreating a simple entity

framework application.

18

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

19

Inheritance is a good thing

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

Two types are currently supported

20

DEMOSimple Inheritance

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.4 natively supports in/out and out parameters

• Best practice is to make your schema look as much like SQL Server as possible!

22

Planned support

• We are committed to supporting Entity Framework, LinQ and other MS data-centric initiatives.

• Connector/Net 6.0 is out now!– Has support for Entity Framework 1.0

• Connector/Net 6.1 will not include native LinQ support– We had originally planned this but decided on other priorities

– Targeting 6.3 for some form of LinQ support

– Very interested in enabling LinQ on Mono!

23

What We Suggest:

Learn about the most popular open-source database.> http://www.sun.com/mysql

Free trial offer — MySQL Enterprise.> http://www.sun.com/mysql/trial

Take advantage of the Enterprise Unlimited Server offer.> http://www.sun.com/mysql/unlimited

Try before you buy, then save.> http://www.sun.com/tryandbuy/specialdetail.jsp?spid=f17eb968-ce40-48c5-9f63-5deb8e339216

Save on a Sun server/storage bundle.> http://www.sun.com/specials/g/bundleofsun.xml?intcmp=1390

24

Resources•Webinar replay (posted in three days)–http://mysql.com/news-and-events/on-demand-webinars/

•Upcoming Webinars>“For ISVs: What's New in MySQL Connector/NET 6.1”Tues 15 Sept, 10 am PT–http://mysql.com/news-and-events/web-seminars/display-407.html

>“For ISVs: Architecting Embedded Databases for High Performance Applications”, Wed 23 Sept, 10 am PT–http://mysql.com/news-and-events/web-seminars/display-405.html

•MySQL ISV / OEM Customers–http://mysql.com/customers/embedded/

•Chance to Win A $150 Amazon.com Gift Certificate!>Just complete MySQL's 3 minute survey on your product's development cycle–http://www.zoomerang.com/Survey/survey.zgi?p=WEB22957RJFHET

•Contact MySQL–http://mysql.com/about/contact/sales.html?s=oem

top related