Absolute Beginners Guide to Entity Framework

image 

The Essentials

The Entity Framework bridges the gap between how developers commonly manipulate conceptual objects (customers, orders, products; posts, tags, members; wall posts, private messages, friend connections) and the way data is actually stored (records in database tables).  The technical term for a tool that provides this abstraction is object relational mapper (ORM).  ORMs help developers be more efficient and focused, since they don’t need to spend brain cycles thinking about how to communicate with the database.  It also means that the code is more portable – switching database software requires changing a setting in the ORM, not a rewrite of the whole codebase to match the new database’s dialect.  As someone who has programmed using the ORM in Django, I can tell you how such a tool makes development less tedious and more enjoyable when you don’t have to consider SELECTS and INSERTS.  In fact, I’ve never before written a line of SQL, yet I was able to build a rich web application thanks to an ORM.

Getting Started

One of the nice features of using the Entity Framework is its out-of-the-box simplicity.  To my pleasant surprise, I didn’t need to download, install, or patch anything to get started with my first EF app.  I booted up VS 2010, opened a new project, designed my entities, wrote my code, and off I went.  You can probably get a very simple app up and running in about 10 minutes.  To prove it, I’ll do exactly that as a step-by-step walkthrough below.  For this example, I’ll make a simple events calendar.

Read more … Absolute Beginners Guide to Entity Framework – ADO.NET team blog – Site Home – MSDN Blogs

Code-First Development with Entity Framework 4

image image

Also my older post: https://vincenthomedev.wordpress.com/2010/07/15/simple-code-first-with-entity-framework-4/

Simple Code First with Entity Framework 4

 

Microsoft announced a new Entity Framework CTP today.

It’s got a lot of interesting features and choices, and while it’s still a CTP, you should take a minute and check it out. 

.NET 4 ships with a much improved version of Entity Framework (EF) – a data access library that lives in the System.Data.Entity namespace.

Some of the big improvements in EF4 include:

  • POCO Support: You can now define entities without requiring base classes or data persistence attributes.
  • Lazy Loading Support: You can now load sub-objects of a model on demand instead of loading them up front.
  • N-Tier Support and Self-Tracking Entities: Handle scenarios where entities flow across tiers or stateless web calls.
  • Better SQL Generation and SPROC support: EF4 executes better SQL, and includes better integration with SPROCs
  • Automatic Pluralization Support: EF4 includes automatic pluralization support of tables (e.g. Categories->Category).
  • Improved Testability: EF4’s object context can now be more easily faked using interfaces.
  • Improved LINQ Operator Support: EF4 now offers full support for LINQ operators.

Visual Studio 2010 also includes much richer EF designer and tooling support. The EF designer in VS 2010 supports both a “database first” development style – where you construct your model layer on a design surface from an existing database.  It also supports a “model first” development style – where you first define your model layer using the design surface, and can then use it to generate database schema from it.

To get a more detailed version of this walkthrough plus downloadable sample code, check out the ADO team’s excellent blog post.

Code-First Development with EF

In addition to supporting a designer-based development workflow, EF4 also enables a more code-centric option which we call “code first development”.  Code-First Development enables a pretty sweet development workflow.  It enables you to:

  • Develop without ever having to open a designer or define an XML mapping file
  • Define your model objects by simply writing “plain old classes” with no base classes required
  • Use a “convention over configuration” approach that enables database persistence without explicitly configuring anything
  • Optionally override the convention-based persistence and use a fluent code API to fully customize the persistence mapping

EF’s “code first development” support is currently enabled with a separate download that runs on top of the core EF built-into .NET 4.  CTP4 of this “code-first” library shipped this week and can be downloaded here

It works with VS 2010, and you can use it with any .NET 4 project (including both ASP.NET Web Forms and ASP.NET MVC).

Step by Step Tutorial: Building NerdDinner using a Code-First Approach

image

We will walkthrough building this application step-by-step. Goto: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

Also http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx

Choosing a Data Access Layer for Silverlight 3

 

Choosing a Data Access Layer for Silverlight 3

Silverlight

The three major candidates in Silverlight 3 are Web Services (WCF/ASMX), ADO.NET Data Services and RIA Services.  In any situation, any of these will work. But they are suited to different styles and requirements. Here’s the abridged differences between the stacks:

  • Web Services: Interface-based Data Access
  • ADO.NET Data Services: LINQ-based Data Access with change management
  • RIA Services: Interface-based Data Access with change management

Read More here … Shawn Wildermuth – Choosing a Data Access Layer for Silverlight 3

Building RESTful application using SQL Azure, WCF, ADO.NET Data Service & Telerik OpenAccess WCF Wizard

 

 

PUT vs POST in RESTful Web Service

Let us use a bookmark service as an example.

 

image

We can use PUT to create new user accounts because the client is the one picking the username that forms the new resource’s URI. If the client is successful, the service will return a 201 (“Created”) response. If the client attempts to use an existing username, the service will return a 401 (“Unauthorized”) response. When issuing the PUT request to create a user account, the client will provide a user account representation in the HTTP request body containing the user’s information.

For individual bookmark resources, we’ll support GET, POST, PUT, and DELETE requests. If a particular bookmark is marked as private, only the owner can retrieve it, but if it’s public, anyone can retrieve it. When a user creates a new bookmark, the service is responsible for assigning it a unique bookmark Id. Hence, the client won’t know the Id to use in the URI ahead of time. So, instead of using a PUT request, we’ll have users POST bookmarks to the user’s bookmark collection resource.  The handler for the POST request will create the new bookmark, assign it an Id, and return a 201 (“Created”) to the client, specifying the URI of the new bookmark resource in the Location header.

This explains when to use POST or PUT for creating new resources. The answer ultimately lies in who is responsible for determining the new resource’s URI. If the client is in charge, the client can use PUT to the new URI (like we did for user accounts) and the service can return a response code of 201 (“Created”). However, if the service is in charge of generating the new URI, the client should POST the new resource to a factory URI like we’ve done for bookmarks. Then, the service can return a response code of 201 (“Created”) along with the URI of the new resource in the response “Location” header.

Once clients know the bookmark ID’s, they can issue PUT and DELETE requests to individual bookmark URIs to update and delete bookmarks.

Dynamic Data in a Nutshell

 

What is ASP.NET Dynamic Data?

image

http://www.asp.net/dynamicdata/

Fundamentally, Dynamic data is new controls for DetailsView, FormView, GridView, or ListView controls along with a lot of meta-infrastructure classes to make it possible. The design uses convention over configuration in a number of places, and this marks, IMHO, the recognition of a simpler way to do things.

If you can point at one thing in Dynamic Data and say, “that’s it!”,  it’d be DynamicControl. it’s a control that takes metadata from your Database model (LINQ to SQL or LINQ to Entities in v1, other POCOs (Plain Old CLR Objects) or ORM’s possibly in v.Next) and selects from a FieldTemplateUserControl.

Here’s a comment from Peter Blum left on ScottGu’s Blog.

Dynamic Data introduces a new web control called DynamicControl. It can be used in FormView and ListView controls to provide a UI based on database-level schema. When using GridView and DetailsView, you add a DynamicField, which is a replacement for the BoundField that internally has a DynamicControl. (Thus has all of the features of a DynamicControl.)

This results in your Grids and data entry forms being built around tables and columns of your database with whatever smart data entry controls you like, such as the AJAX Control Toolkit’s MaskedTextBoxExtender on a textbox or your favorite third party controls. The database schema also determines the validators for this column. That avoids errors implementing the right validators or updating them when the database schema is modified. (No more “how do I validate a BoundField?” issues!)

To me, Dynamic Data is a major step forward in developing data entry oriented web applications for ASP.NET.

One of the demo’s that is often shown is Dynamic Data’s “scaffolding” feature. Basically, you point the wizard at a database and “poof” you’ve got a nice DataGrid Admin-like site. However, to dismiss it as a scaffolding tool is just the mistake I made when I first saw it.

How does it work?

Goto  Putting ASP.NET Dynamic Data into Context

Related Links

Windows Communication Foundation (WCF) 3.5 REST Starter Kit

Windows Communication Foundation (WCF) REST Starter Kit

The WCF REST Starter Kit is a set of features, Visual Studio templates, samples and guidance that enable users to create REST style services using WCF. It provides features that enable or simplify various aspects of using the http capabilities in WCF, such as caching, security, error handling, help page support, conditional PUT, push style streaming, type based dispatch and semi-structured XML support. The WCF REST Starter Kit also provides Visual Studio templates for creating REST style services such as an Atom feed service, a REST-RPC hybrid service, Resource singleton and collection services and an Atom Publishing Protocol service. In addition, there are samples, documentation and other guidance that help to use these new capabilities.

Download WCF REST Starter KitDownload

Learn about WCF REST Starter Kit

Via Windows Communication Foundation (WCF) REST Starter Kit

In the case of REST Singleton Web Service, using jQuery to do AJAX is as simple as:

<script type=”text/javascript”>
    $(function() {
        $.getJSON(“http://localhost:62537/Service.svc/?format=json”, null, function(json) {
            $(‘#result’).html(json.Value);});  
    })
</script>

ASP.NET Dynamic Data

 

In a nutshell this enables you to really quickly build data driven web-sites that work against a LINQ to SQL (and in the future LINQ to Entities) object model – and optionally allows you to-do this without having to build any pages manually.

The best way to see this in action is to watch David Ebbo’s awesome 17 minute screen-cast:

Via New ASP.NET Dynamic Data Support – ScottGu’s Blog