Streamlining Python Development: A Guide to a Modern Project Setup
Entity Framework 4 In Microsoft Visual Studio 2010
1. ADO.NET Entity Framework in Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 Eric Nelson [email_address] http://geekswithblogs.net/iupdateable http://twitter.com/ericnel Developer Evangelist Microsoft UK Advert Alert http://ukazure.ning.com
Look at the problems that EF addresses: - opaque database commands buried in strings - tedious, repetitive code to materialise objects Create connection SQL String – embedded joins DataReader – not type safe
Type safe code No joins as we’ve abstracted away the database representation. (If we had joins then the compiler could verify them) Familiar query syntax (L2O, L2Xml, ...) Working with objects
Some of the features of EF v1... Abstract the db model. Get a richer object/entity model to work with in the application. Isolate from change. Work with objects not tables. Have object relationships. Type-safe queries. Easy to query across relationships (just access properties on the objects!). Compile-time checking. Pretty good start, some rough areas. Mention vote of no confidence
Yep, apparently “4” is the number after “1” in marketing ;-) Aligning version numbers with .NET 4, C#4. Don’t be alarmed if you see a reference to EF4!
Code Gen In V1 we had EntityClassGenerator which could be configured using events Hard (it used CodeDom) Inflexible (not much control) In V2 we will have TemplatedEntityClassGenerator and will ship default T4 templates Customization via Tools Uses Workflow Foundation Complex types Pluralization Public abstract PluralizationService Default implementation is English Only Default rules: EntityTypes / ComplexTypes are singularized EntitySets are pluralized Navigation Properties based on cardinality Stored Procedures Stored Procedures as functions Mapping support for stored procedure result Complex type as return type Scalar and void return type Entity CUD using Stored Procedures No need to have SPs for all CUD
Foreign Keys Independent Association – v1 Product p = new Product { ID = 1, Name = &quot;Bovril&quot;, Category = context.Categories .Single(c => c.Name == &quot;Food&quot;) }; context.SaveChanges(); FK Association – v2 Product p = new Product { ID = 1, Name = &quot;Bovril&quot;, CategoryID = 13 }; context.Products.AddObject(p); context.SaveChanges(); Deferred Loading Can do lazy load Context.deferredloading=true Model Level using ESQL <Function Name=“CustomerFullName” ReturnType=“String”> <Parameter Name=“customer” Type=“MyModel.customer”> <DefiningExpression> customer.FirstName + ‘ ‘ + customer.LastName </DefiningExpression> </Function> Enables from c in ctx.Customers select c.FullName() CLR Level [EdmFunction(&quot;MyModel&quot;, &quot;CustomerFullName&quot;)] public static string FullName(this customer c) { return String.Format(&quot;{0} {1}&quot;, c.FirstName, c.LastName); } Enables Console.WriteLine(c.FullName()); Inline Functions in ESQL using Northwind; function AmountPurchased(c Customer) as Sum (c.Sales.Amt) function AmountReturned(c Customer) as Sum (c.Returns.Amt) function AmountConsumed(c Customer) as AmountPurchased(c) – AmountReturned(c) select AmountConsumed(c) from Customers as c;
Before going continuing, should mention the Feature Pack. Not all of the features I’m covering here will be in EF4 RTM, some (as mentioned in slide) will form the Feature Pack…
Code Only No XML files, no model! Convention to config E.g. Convention: xxxID to a primary key, schema=dbo E.g. Config: On map to schema sys and class property to table column [TableMapping(Schema=“sys”,TableName=“MyDBTable”] [ColumnMapping(PropertyName=“MyClassProp1”, ColumnName=“table_column1”)] [Key(PropertyName=“MyClassProp1”)] public objectset<MyThing> MyThings...
Db First (v1 approach) Model first. Start from scratch in designer. Generate code and database from model. Database generation is driven via a workflow that you can replace (haven’t looked at this yet!) Code first (feature pack). You create your classes in code and add some code to tell EF about the classes and their relationships. EF essentially creates a model internally and creates the database for you.
N-Tier – exposing EF classes via WCF. Lots of feedback around the difficulties with detaching/reattaching object from context New API to makes this easier ( context.ObjectStateManager.ChangeRelationshipState ). Self-tracking Entities T4 template to make this even easier. Click through the example. Refer to great sample app by Danny Simmons for MSDN Magazine. N.B. Self tracking entity functionality requires type sharing between service and client