I have been following TechEds since 2005. This time India TechEd 2011 is very special to me. I am delivering session called
Data Access Methodologies: When to choose what (ADO.NET, Entity Framework, LINQ, WCF Data Services) and the official abstract says,
Often an Application requires to access database. A lot of evolving data access technologies are available today from Microsoft. Choose the right one for right type of your application (Windows Forms, Windows Presentation Foundation, ASP.NET, ASP.NET MVC, Silverlight etc). Find which one is the best fit for you from ADO.NET, LINQ, ADO.NET Entity Framework, and WCF Data Services. This session will also highlight on the future investments of data access technologies from Microsoft and help you decide the right one.
With this I am very excited to talk about different data access technologies available today and what you need to know when to choose what. This talk will give you an idea about what is there in various different options we have today. We will focus on ADO.NET Entity Framework v4 and WCF Data Services. Hoping to make it a demo intensive talk - to me 1 hour is very less to cover this most debated area and moreover to a audience of experienced industry folks.
I need your best of luck for my session at TechEd 2011 - pray for the projector and demo to run smoothly as expected.
Wish you to see there and connect personally.
Namoskar!!!
Sometimes you may want to use same logic again and again. Let’s suppose you do not have access to your database which allows you to write function or stored proc. Never mind, EF allows you to define the logic at Model and you should be able to experience the same.
You want to search an employee with its id. Open the edmx file in XML view and add the the below code under <schema>
<Function Name="GetEmployeeFN" IsComposable="false"> <CommandText> Select * from Emp WHERE EmpId = @id </CommandText> <Parameter Name="id" Type="int"></Parameter> </Function>
And in the model browser you will find it just like when you import and SQL Stored Proc
After that do a simple function import and get the power.
using (TestDBEntities db = new TestDBEntities()) { foreach (var k in db.GetEmployeeFN(41)) { Console.WriteLine(k.LastName); }
How to: Build a Windows Azure Application How to Configure Virtual Machine Sizes How to Configure Connection Strings How to Configure Operating System Versions How to Configure Local Storage Resources How to Create a Certificate for a Role How to Create a Remote Desktop Protocol File How to Define Environment Variables Before a Role Starts How to Define Input Endpoints for a Role How to Define Internal Endpoints for a Role How to Define Startup Tasks for a Role How to Encrypt a Password How to Restrict Communication Between Roles How to Retrieve Role Instance Data How to Use the RoleEnvironment.Changing Event How to Use the RoleEnvironment.Changed Event
How to: Use the Windows Azure SDK Tools to Package and Deploy an Application How to Prepare the Windows Azure Compute Emulator How to Configure the Compute Emulator to Emulate Windows Azure How to Package an Application by Using the CSPack Command-Line Tool How to Run an Application in the Compute Emulator by Using the CSRun Command-Line Tool How to Initialize the Storage Emulator by Using the DSInit Command-Line Tool How to Change the Configuration of a Running Service How to Attach a Debugger to New Role Instances How to View Trace Information in the Compute Emulator How to Configure SQL Server for the Storage Emulator
How to: Configure a Web Application How to Configure a Web Role for Multiple Web Sites How to Configure the Virtual Directory Location How to Configure a Windows Azure Port How to Configure the Site Entry in the Service Definition File How to Configure IIS Components in Windows Azure How to Configure a Service to Use a Legacy Web Role
How to: Manage Windows Azure VM Roles How to Create the Base VHD for a VM Role in Windows Azure How to Install the Windows Azure Integration Components How to Enable Windows Azure Connect for a VM Role How to Develop an Adapter for a VM Role in Windows Azure How to Prepare the Server Image for Uploading to Windows Azure How to Upload a VHD to Windows Azure How to Create and Deploy the VM Role Service Model How to Use Certificates With a VM Role in Windows Azure How to Change a Server Image for a VM Role by Using a Differencing VHD How to Change the Configuration of a VM role How to Manage the Lifecycle of VM Role Instances in Windows Azure
How to: Administering Windows Azure Hosted Services How to Setup a Windows Azure Subscription How to Setup Multiple Administrator Accounts
How to: Deploy a Windows Azure Application How to Package your Service How to Deploy a Service How to Create a Hosted Service How to Create a Storage Account How to Configure the Service Topology
How to: Upgrade a Service How to Perform In-Place Upgrades How to Swap a Service's VIPs
How to: Manage Upgrades to the Windows Azure Guest OS How to Determine the Current Guest OS of your Service How to Upgrade the Guest OS in the Management Portal How to Upgrade the Guest OS in the Service Configuration File
How to: Manage Management Certificates How to Create a Management Certificate How to Add a Management Certificate to a Windows Azure Subscription
How to: Manage Service Certificates How to Add a New Certificate to the Certificate Store How to Associate a Certificate with a Service How to Update a Certificate in the Certificate Store How to Configure an HTTPS Endpoint in Windows Azure How to Control Access to Certificates on a Virtual Machine
How to: Use Storage Services How to Create a Storage Account How to Read Configuration Settings for the Storage Client Library and Handle Changed Settings
How to: Configure Windows Azure Connect How to Activate Windows Azure Roles for Windows Azure Connect How to Install Local Endpoints with Windows Azure Connect How to Create and Configure a Group of Endpoints in Windows Azure Connect
How to: Manage CDN on Windows Azure How to Enable CDN for Windows Azure How to Map CDN Content to a Custom Domain How to Remove Content from the CDN How to Manage Expiration of Blob Content How to Manage Expiration of Hosted Service Content
You can download it today by going here:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=752CB725-969B-4732-A383-ED5740F02E93&displaylang=en
· Hands-on Lab - Migrating a Windows Forms Application to Silverlight
· Hands-on Lab - Migrating an ASP.NET Web Forms Application to Silverlight
· Hands-on Lab - Working with Panels, XAML and Controls
· Hands-on Lab - Silverlight Data Binding
· Hands-on Lab - Migrating Existing Applications to Out-of-Browser
· Hands-on Lab - Great UX with Blend
· Hands-on Lab - Web Services and Silverlight
· Hands-on Lab - Using WCF RIA Services
· Hands-on Lab - Deep Dive into Out of Browser
· Hands-on Lab - Using the MVVM Pattern in Silverlight Applications
Windows Azure
· Hands-on Lab – Introduction to Windows Azure
· Hands-on Lab – Debugging Applications in Windows Azure
· Demo Script – Hello Windows Azure
· Demo Script – Deploying Windows Azure Services
· Presentation – Windows Azure Platform Overview
· Video – What is Windows Azure?
Session 01: Windows Azure Overview Session 02: Introduction to Compute Session 03: Windows Azure Lifecycle, Part 1 Session 04: Windows Azure Lifecycle, Part 2 Session 05: Windows Azure Storage, Part 1 Session 06: Windows Azure Storage, Part 2 Session 07: Introduction to SQL Azure Session 08: Windows Azure Diagnostics Session 09: Windows Azure Security, Part 1 Session 10: Windows Azure Security, Part 2 Session 11: Scalability, Caching & Elasticity, Part 1 Session 12: Scalability, Caching & Elasticity, Part 2, and Q&A
In my case I may be doing a lot of save under one method and wanted to ensure if I am doing update for one single entity (table) it should prevent me to do it. I have added a table to the database with a TimeStamp column. The idea of adding the TimeStamp column is to have a column which will be changed with every modification. You could also use any other column.
CREATE TABLE [dbo].[Emp_Concurrency]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](50) NULL, [TimeSt] [timestamp] NULL, CONSTRAINT [PK_Emp_Concurrency] PRIMARY KEY CLUSTERED ( [Id] ASC ) ) Now in the model change the property of TimeSt’s Concurrency Mode = Fixed
Now when I run the below code
Emp_Concurrency ec = new Emp_Concurrency() { Name = "A" }; //Adding new Employee using (TestDBEntities db = new TestDBEntities()) { db.Emp_Concurrency.AddObject(ec); db.SaveChanges(); } using (TestDBEntities db = new TestDBEntities()) { var eC = (from e1 in db.Emp_Concurrency where e1.Id == ec.Id select e1).First(); //Console.WriteLine(eC.Name); //Update using Model - but not yet applied eC.Name = "New Name 1"; //Update using Query - this will try to update the db db.ExecuteStoreCommand("Update Emp_Concurrency SET Name='LL' WHERE Id = {0}",ec.Id); try { db.SaveChanges(); } catch (OptimisticConcurrencyException exceptionC) { Console.WriteLine("Concurrency Error!{0}", exceptionC.Message); } catch (Exception ex) { Console.WriteLine("Exception! {0}", ex.Message); } }
Now you get the below exception
Concurrency Error!Store update, insert, or delete statement affected an unexpect ed number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
Concurrency Error!Store update, insert, or delete statement affected an unexpect
ed number of rows (0). Entities may have been modified or deleted since entities
were loaded. Refresh ObjectStateManager entries.
Over a period of time I have been working with ADO.NET Entity Framework and I am flattered with its amazing usability. I am a big fan of EF and especially with EF4 release a lot more avenues are open. I love to talk about this technology in any forum and given a chance I would love to work on any implementation and migration. While doing all of these stuffs I found one big question which always comes to our mind is performance and it is quite obvious that for any data access technology performance is a big concern.
Below are my take on EF’s performance considerations I found could be helpful. Please feel free to add your thoughts and I would like to keep the option open to enhance the experience better for others.
Use CompiledQuery
CompiledQuery is a great way to fetch data. So if you have a query which you are planning to use again and again with search filter, you should create a compiled query for it. Remember one thing very clearly while defying CompiledQuery is - CompiledQuery takes a while to get generated for the first time and next time onwards it is really fast. So do not expect it to be faster than the normal LINQ.
Please declare CompiledQuery as static (shared in VB), otherwise the benefit of using CompiledQuery is wasted. Below is a small example of using CompiledQuery in EF
Avoid using DTC Transaction
Avoid the use of Windows Distributed Transaction Coordinator with EF. Because performance pays for DTC. There would be cases where you have to use it. So think twice before you implement System.Transactions.TransactionScope. Please refer my blog on it http://blogs.msdn.com/b/wriju/archive/2011/03/14/ado-net-entity-framework-transaction.aspx
Use MergeOptions.NoTracking
When simply reading data from database and not modifying it, makes no sense to use the identity management which is default. So you need to switch it off explicitly. So it is always better to set as below while doing read only.
ctx.Emps.MergeOption =
or
((
Shape the Query - as needed
When I mentioned “shape query” - it means you need to choose when to use Lazy Loading or Immediate Loading. In EF Lazy loading is default enabled while using designer. However you can switch it off.
Immediate Loading: If you are using the loading time of your application to load some user settings you should load of some of the data as well. This would avoid multiple round trips. However, this may be valid for thick client applications. The way to use immediate Loading is to use include method.
var
Lazy Loading: Is default enabled if using designer and useful in most of the cases (mainly for WebSites or shared memory) . But you can always change it by setting ctx.ContextOptions.LazyLoadingEnabled = true;
Manage Connection
You should manually open or close connection if you are using SaveChanges() multiple times under same context. Because SaveChanges automatically open and close connection as and when called. Manually opening and closing connection would help you avoid those situations.
ctx.Connection.Open();
Use Pre-Generated View
Using pre-generated view reduces the loading time. For more detailed discussion and how to please refer http://msdn.microsoft.com/en-us/library/bb896240.aspx
By choosing ORM you make your life simple on the other hand you have to pay for it as it uses extra overhead for being layered.
I am very excited to see the Visual Studio 2010 SP1. You can download this from http://go.microsoft.com/fwlink/?LinkId=209902.
The article http://go.microsoft.com/fwlink/?LinkId=209902 talks about what is there and some of my picks are
And a lot of open issues have been resolved too.
Any data-driven application needs Transaction and to ensure that the unit of work is either together successful or fail. Transaction plays a very important role. Assume that we are working with SQL Server. Now SQL Server has it’s own Transaction mechanism which could be used in Stored Procedures. However, not every time a Transaction is just about adding or deleting values to multiple database tables. It could also get involved into writing to a local system file or registry along with database manipulation. So challenge comes when you have both of them together and want to ensure if a registry access is denied then database commit has to be rolled back. At times you need to add value to database first to get the Identity field value and then right to registry. So if you do not save value to a database the you won’t get the identity field value. But the challenge is how to revert that committed change from data.
Windows Distributed Transaction Coordinator (DTC) comes very handy in those cases. However DTC is quite expensive and needs to be up and running on both database and application machines.
ADO.NET EF uses Transaction by default. For SQL Server it uses SqlTransaction. So need to bother about it if you are working on single table and referential integrity issues are occurring.
To use TransactionScope class you need to keep two things in mind
Now below code saves one entry to one database and then reads the newly generated EmpId and saves to another database
Now if you observe the Sql Profile you will find few interesting information
SQLTransaction gets promoted to DTC and then DTC takes the charge.
Earlier it was concern with a many customers regarding the credential sharing. Generally big bosses used to purchase the azure account and developers were using it. Then only one Live Account was allowed to login to one subscription, they had to share the user id and password which would not be an acceptable way. Now, things have become much more simple and you can add co-owner to your portal and they can use their Live id to access and share the same subscription.
After that add the user
A step by step guidance http://blogs.technet.com/b/gmarchetti/archive/2011/03/12/put-a-vm-on-azure.aspx
Every month you get a bill of your Windows Azure Platform usage. Below is the guide to help you understand the inside of it. You may also find some unused Hosted Service causing the pic of your bill.
http://www.microsoft.com/windowsazure/support/understandbill/
If you are a register Windows Azure user, you can check your bill at https://mocp.microsoftonline.com
How to deploy Application to new Windows Azure 1.3 portal. This channel 9 video will walk you through step by step.
http://channel9.msdn.com/posts/AzureDeploy
Latest and updated resources on Windows Azure is here http://wriju.cloudapp.net/