I have recently added one video in Channel 9 at http://channel9.msdn.com/Blogs/wriju/Team-Foundation-Server-2010--Creating-Team-Project
I love the new look of Channel 9.
Namoskar!!!
More on Developer Details at http://msdn.microsoft.com/en-us/ie/ff468705.aspx
While Installing…..
After it installs the IE9 the machine will be restarted and you will get a super cool browser.
IE 9 Rocks!!!
Internet Explorer 9 or IE9 is simply magic and launched today (Sept 15, 2010). Download it from here at http://windows.microsoft.com/en-US/internet-explorer/download/ie-9/worldwide
Lots of canned demos are at http://ie.microsoft.com/testdrive/. I personally liked the HTML 5 power http://ie.microsoft.com/testdrive/
Most of the time we struggle to get some dummy data for various types of testing. Data Generation Plan in Visual Studio 2010 helps to achieve it. It not only fills the tables based on the number of rows selected by us it also takes care of the relationship if any. One thing to remember is about the quality of data, all junk.
Let’s suppose you have a database project of Northwind. Now you need to create some 5000 categories to do a Load Testing. Very easy to achieve,
Database Project > Add New Item > Data Generation Plan. Then choose the number of Rows.
Once done you can choose to run by hitting F5 or use menu,
Your dummy data is ready, please do not try to ready it :).
To answer the common questions related to MSDN is available here http://www.microsoft.com/downloads/details.aspx?FamilyID=2b1504e6-0bf1-46da-be0e-85cc792c6b9d&displaylang=en
When you have two SQL Sever Databases and need to find the differences, Visual Studio comes really handy.
Under Visual Studio 2010’s menu Data > Schema Compare. Once you choose New Schema Comparison
Then you choose your databases. After you choose it you can also set the Schema Compare Settings. Once you press OK. It gives you the report.
Let’s suppose you have one additional column in your database, it shows something,
You can further drill-down to find the details,
Now this also gives you the Script differences with highlight.
Then you can choose Write Update from menu
This will update you right selection (Target).
Developing Applications for SharePoint 2010
Brief Description
This guidance provides technical information about key areas of SharePoint 2010 application development. It contains a written guide, reference implementations with source code and documentation, and a library of reusable source code components.
http://www.microsoft.com/downloads/details.aspx?familyid=64B55569-2168-4545-8B7C-F185B2CF967D&displaylang=en
SharePoint 2010 Walkthrough Guide
This walkthrough guide is designed to provide a step-by-step overview of Microsoft® SharePoint® 2010.
http://www.microsoft.com/downloads/details.aspx?familyid=8C619BEF-008B-4AF2-9687-8A05848FEA97&displaylang=en
I have posted one blog on Complex Type and got overwhelming responses. Thanks for keeping me motivated. I also have created one Channel 9 video on Complex Types. Please visit http://channel9.msdn.com/posts/wriju/Complex-Type-ADONET-Entity-Framework/
Today I have published one video at Channel 9 on POCO Template. Please find them here http://channel9.msdn.com/posts/wriju/POCO-Template-in-ADONET-Entity-Framework/
Do you want to learn Web Technologies? ASP.NET MVC 2, ASP.NET 4 Web Forms, jQuery, Entity Framework using Visual Studio 2010 with lots of Demos, Hands-On-Labs and scenario based content.
Download and start using today from http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=0a906559-28bd-4f5d-ac18-f895a200a2a5
This is not new in Visual Studio 2010. However this could be helpful while creating WCF based application. You may like to test your application from Visual Studio 2010 by hitting F5. By default, we can make only one project as Startup Project.
Select the solution and right click and select
When we need to work with an Entity Framework model and use POCO. We need to get rid of the designer generated code. But how? There are few ways by which we can achieve it.
Option 1
I personally like this approach. You need to open the Model designer and click F4. The change the “Code Generation Strategy” to “None”. Benefit of this approach is that this is drop down option and you can get the designer generated code back anytime later.
Option 2
Under Model’s file property (as in pic) change the “Custom Tool” property “” from “EntityModelCodeGenerator”. If you remove it, then of required later you have to type the whole text to this property value.
Is to simplify your life which might seem too complex :). Let’s suppose you have a table like,
Now you might want to club all address related field and make one single representation to it. Once you have created the model in your Visual Studio 2010, you may just need to select all the properties and choose the option to create the Complex Type.
Once you have done it, the Model Browser and Model would look like,
Your query to fetch the required data then would look like,
What the heck!!! Then finally Amar did help me to get it done. Simple “option”. Life has too many “options”.
Tools > Options
You need to just uncheck the option “Prevent saving changes that require table re-creation”
This is exactly Scott Hanselman communicated on which we live our everyday life. How were those days when there were only books and different CDs of MSDN. Then Google and Bing came into the picture. Being a developer how we drink resource and make them useful for others.
http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-Social-Networking-for-Developers-Part-1-Every-Developer-Needs-a-Blog/
Every developer should watch it !!!
There are a lot of buzz around the Foreign Key support in ADO.NET Entity Framework v4. Question often comes to our mind that what is so special here? While discussing with my fellow Consultant Vijay, he gave me a nice example. Let me share here,
Let’s assume that you have two tables Dept and EmpDept. Connected with DeptId which is Primary in Dept table and Foreign key in EmpDept table. When we simply use SQL query we can easily change the DeptId of an employee by sending one single Update statement like
This simple thing was totally missing in the previous version of EF (in Visual Studio 2008). We had to pull out the whole new Dept to update an existing EmpDept.
If we have the database table like,
From it if we have built the model like, (as it was in Visual Studio 2008)
Notice, here you do not have the DeptId as static property in the EmpDept entity. Thus the pain. So to simply shift one employee from one dept to another you had to write something like,
Which would execute 3 queries in the database.
Now, in EF4 you get the foreign key as static column. So things becomes much more simpler both from the front end code perspective and backend execution. The new model looks like as below if we select the “Include foreign key columns in the model”
as,
Now notice here the new static property in EmpDept as DeptId. If you want to achieve the same goal here, things would become much more simpler,
And also the generated SQL would be only 2,
I personally still feel that being ORM we are paying the additional database roundtrips here. And if you want to reduce it again to simple one either you use Stored Procedure and create Function or execute RAW T-SQL using ExecuteStoreQuery method of Context.
If you are just trying to fetch the data and has no intention to update it. Then you should be using the MergeOption.NoTracking to ensure that the ObjectStateManager does not store the required information for update and delete. Hence your query would be more quick in nature.
So this simple line would add a lot of value to your performance and clearly visible in second call onwards.
If we are using similar query frequently, we can increase it’s performance by Compiling it through CompiledQuery. It’s always recommended to use CompiledQuery if you happen to see the query is getting executed many times.
Let’s take an example, in Northwind database if you are getting Customer based on City. You may follow the below approach. Also a very important point to observe is to see how it runs faster in subsequent calls even though the parameter values differ.
Now, instead of writing raw LINQ use this Compiled Query
We also wanted to capture the time.
Just call twice to capture the time,
In my machine the first call took 322 milliseconds and the second one took 4 milliseconds. This may differ time to time and machine to machine. But this is faster indeed.
ADO.NET Entity Framework 4.0 allows us to easily create N-Tier Application with the help of Self-Tracking Entities. This means now we do not have to worry about merging and checking values in more disconnected scenario. Here is how we can do it in very simple example to start with.
Create Model Layer
· Create a blank solution in Visual Studio 2010 with the name NTier.
· Create a new Class Library application. Give the name TestDBModel.
· Right click to the TestDBModel project and choose Add New Item. Add ADO.NET Entity Data Model and name it “TestDBModel.edmx”.
· Point to the database and table (here it is Emp)
· The Emp table is simple and has
CREATE TABLE [dbo].[Emp]( [EmpId] [int] IDENTITY(1,1) NOT NULL, [EmpName] [varchar](50) NULL, CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED ( [EmpId] ASC )
CREATE TABLE [dbo].[Emp](
[EmpId] [int] IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](50) NULL,
CONSTRAINT [PK_Emp] PRIMARY KEY CLUSTERED
(
[EmpId] ASC
)
· Then select the TestDBModel.edmx from the designer and go to the property window and select the property “Code Generation Strategy” and change it to None. The reason behind doing it is to get WCF enabled entity classes which can keep the track of changes made across the layers without having an open connection (context).
· Then right click on the TestDBModel.edmx designer and select “Add Code Generation Item”.
· Choose “ADO.NET Self-Tracking Entity Generator” and name it Model1.tt.
· If you explore it you will find the context and Emp class created by that template already. You do not have to write/change anything there.
Create Service Layer
· Now create a WCF Class Library project with a name WcfServiceLibrary1.
· Add a class file ITestDB.cs with the following code.
· Now add one more class file TestDBService.cs with the following code,
· You also need to bring the EF connection string from the model project to this project’s App.config file.
Create UI Layer
· Create a Windows Forms Application with a name WindowsFormsApplication1. Add Service reference to your WCF Class Library.
· Then add Grid View named dataGridView1 and Button named button1.
· Add the following code,
private void button1_Click(object sender, EventArgs e){ var svc = new ServiceReference1.TestDBClient(); dataGridView1.DataSource = svc.GetEmp();} I also have added the code here (check below of this post if you have clicked the post title to read it)
I also have added the code here (check below of this post if you have clicked the post title to read it)
Check out who all using at http://msdn.microsoft.com/en-us/data/ff625830.aspx
I have published one video at Channel 9 on Model First approach.
http://channel9.msdn.com/posts/wriju/Model-First-in-ADONET-Entity-Framework-4/
We are working with Relational Database and using ADO.NET Entity Framework 4.0, it is inevitable that we need to load data. EF4 has improved Lazy Loading feature as compared to its previous version. We can also combine both Lazy and Eager loading.
Based on the amount of data we bring in and type of application we develop, we need to decide which one to choose. There is no single solution.
I have used two tables from Northwind database – Customer and Order (1 to many).
Now, each Customer will have n number of orders, so ideal for Lazy or Immediate loading. Where is the catch? This uses a feature introduced in ADO.NET 2.0 known as MultipleActiveResultSets (MARS). This gets added to the connection string of EF4. Scenario 1: When Lazy loading is enabled (default) By default in EF4 Lazy Loading is enabled through the Model designer. Which means if you write the below code, to get for every Order, it would send a SQL statement to the SQL Server. using (var ctx = new NorthwindEntities()) { var q = from c in ctx.Customers select c; foreach (var cust in q) { Console.WriteLine("Customer : {0}", cust.CompanyName); Console.WriteLine("Respective Orders..."); foreach (var ord in cust.Orders) { Console.WriteLine("Order given on {0}", ord.OrderDate); } Console.WriteLine(); Console.ReadKey(); } } In the background for the first foreach loop it would execute the query in SQL Server to fetch the data from only Customers table. Then for the inner foreach loop it would run individual query to get respective set of orders for each customer. So if there are 100 customers and 1000 orders, the total number of queries would be 1001. Pros and Cons This is acceptable in situations like where you have paged navigation and only when user asks for it, you load it, else leave it. This involves multiple round trips. But when you want to pass data thorough layers, you would not be using it as because there will be no active open context. So when it would try to navigate to the child information no data will be found. Scenario 2: Explicit Loading or conditional Lazy Loading If you want to control the lazy loading on your own by loading the data only if logic requires, you should be disabling the Lazy Loading option and explicitly call it. In the code, you need to add the line ctx.ContextOptions.LazyLoadingEnabled = false; This will switch off the Lazy Loading. //Lazy loading OFF ctx.ContextOptions.LazyLoadingEnabled = false; var q = from c in ctx.Customers select c; foreach (var cust in q) { Console.WriteLine("Customer : {0}", cust.CompanyName); Console.WriteLine("Respective Orders..."); //Conditionally Load the child information if (1 == 1) cust.Orders.Load(); foreach (var ord in cust.Orders) { Console.WriteLine("Order given on {0}", ord.OrderDate); } Console.WriteLine(); Console.ReadKey(); } Pros and Cons This helps us to control the number of roundtrips based on application need. However, we need to be extra careful about testing our code before final deployment. Scenario 3: When Eager Loading is enabled If we switch off the Lazy Loading as discussed in Scenario 2 and want all the records to be available in the memory, then we need to use .Include(Entity). //Lazy loading OFF ctx.ContextOptions.LazyLoadingEnabled = false; var q = from c in ctx.Customers.Include("Orders") select c; foreach (var cust in q) { Console.WriteLine("Customer : {0}", cust.CompanyName); Console.WriteLine("Respective Orders..."); foreach (var ord in cust.Orders) { Console.WriteLine("Order given on {0}", ord.OrderDate); } Console.WriteLine(); Console.ReadKey(); } Pros and Cons This brings all the Customer and Order information via a single query. This is required when we pass data from one layer to another layer. It also reduces the number of roundtrips. However, it can capture large about of your memory space. If we check in the profiler we would see a single query like (removed few characters) SELECT [Project1].[C1] AS [C1], [Project1].[CustomerID] AS [CustomerID],... FROM ( SELECT [Extent1].[CustomerID] AS [CustomerID], ... 1 AS [C1], [Extent2].[OrderID] AS [OrderID], ... CASE WHEN ([Extent2].[OrderID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2] FROM [dbo].[Customers] AS [Extent1] LEFT OUTER JOIN [dbo].[Orders] AS [Extent2] ON [Extent1].[CustomerID] = [Extent2].[CustomerID] ) AS [Project1] ORDER BY [Project1].[CustomerID] ASC, [Project1].[C2] ASC Scenario 4: Combination of Eager and Lazy Loading We could also use .Include and enable lazy loading if we wish to load few data lazily and some one-shot. There is no harm doing it as we are taking the benefit of both the features. MSDN References Loading Related Objects (Entity Framework) http://msdn.microsoft.com/en-us/library/bb896272(VS.100).aspx I would like to present this blog post to my sweet daughter Wrishika and lovely wife Saswati – without them I would have never had so much of energy to continue. Namoskar!!!
Now, each Customer will have n number of orders, so ideal for Lazy or Immediate loading.
Where is the catch?
This uses a feature introduced in ADO.NET 2.0 known as MultipleActiveResultSets (MARS). This gets added to the connection string of EF4.
Scenario 1: When Lazy loading is enabled (default)
By default in EF4 Lazy Loading is enabled through the Model designer. Which means if you write the below code, to get for every Order, it would send a SQL statement to the SQL Server.
using (var ctx = new NorthwindEntities())
{
var q = from c in ctx.Customers
select c;
foreach (var cust in q)
Console.WriteLine("Customer : {0}", cust.CompanyName);
Console.WriteLine("Respective Orders...");
foreach (var ord in cust.Orders)
Console.WriteLine("Order given on {0}", ord.OrderDate);
}
Console.WriteLine();
Console.ReadKey();
In the background for the first foreach loop it would execute the query in SQL Server to fetch the data from only Customers table. Then for the inner foreach loop it would run individual query to get respective set of orders for each customer. So if there are 100 customers and 1000 orders, the total number of queries would be 1001.
Pros and Cons
This is acceptable in situations like where you have paged navigation and only when user asks for it, you load it, else leave it. This involves multiple round trips.
But when you want to pass data thorough layers, you would not be using it as because there will be no active open context. So when it would try to navigate to the child information no data will be found.
Scenario 2: Explicit Loading or conditional Lazy Loading
If you want to control the lazy loading on your own by loading the data only if logic requires, you should be disabling the Lazy Loading option and explicitly call it.
In the code, you need to add the line ctx.ContextOptions.LazyLoadingEnabled = false; This will switch off the Lazy Loading.
//Lazy loading OFF
ctx.ContextOptions.LazyLoadingEnabled = false;
//Conditionally Load the child information
if (1 == 1)
cust.Orders.Load();
This helps us to control the number of roundtrips based on application need. However, we need to be extra careful about testing our code before final deployment.
Scenario 3: When Eager Loading is enabled
If we switch off the Lazy Loading as discussed in Scenario 2 and want all the records to be available in the memory, then we need to use .Include(Entity).
var q = from c in ctx.Customers.Include("Orders")
This brings all the Customer and Order information via a single query. This is required when we pass data from one layer to another layer. It also reduces the number of roundtrips. However, it can capture large about of your memory space.
If we check in the profiler we would see a single query like (removed few characters)
SELECT
[Project1].[C1] AS [C1],
[Project1].[CustomerID] AS [CustomerID],...
FROM ( SELECT
[Extent1].[CustomerID] AS [CustomerID], ...
1 AS [C1],
[Extent2].[OrderID] AS [OrderID], ...
CASE WHEN ([Extent2].[OrderID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM [dbo].[Customers] AS [Extent1]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent2] ON [Extent1].[CustomerID] = [Extent2].[CustomerID]
) AS [Project1]
ORDER BY [Project1].[CustomerID] ASC, [Project1].[C2] ASC
Scenario 4: Combination of Eager and Lazy Loading
We could also use .Include and enable lazy loading if we wish to load few data lazily and some one-shot. There is no harm doing it as we are taking the benefit of both the features.
MSDN References
Loading Related Objects (Entity Framework)
http://msdn.microsoft.com/en-us/library/bb896272(VS.100).aspx
I would like to present this blog post to my sweet daughter Wrishika and lovely wife Saswati – without them I would have never had so much of energy to continue.
This post is to give an idea on how easily we can bind and create master details display in WPF.
Here I am using Northwind sample database and using Category and Products tables. These two tables are connected with each other through CategoryId. We will create a model using these two tables. Please ensure that you check the Foreign Key checkbox while selecting table in the EF4 model creation wizard.
Now, add two controls ListBox with id listBox1 and DataGrid with dataGrid1. Set the AutoGeneratedColumns to checked for the DataGrid.
Now open the Show Data Source from Visual Studio from Data Source menu.
Drop Category to listBox1 and Product (under Category) to dataGrid1. That’s all. Not a single line of code is required.
This post is to give an idea on how easily we can bind and create master details display in Windows Forms Application.
Now, in the Forms1 add two controls. Combo Box with id comboBox1 and Data Grid View with id dataGridView1.
Add the below code
private void Form1_Load(object sender, EventArgs e)
comboBox1.DataSource = ctx.Categories.ToList();
comboBox1.ValueMember = "CategoryID";
comboBox1.DisplayMember = "CategoryName";
And
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
int iCatID = 0;
try
iCatID = (int)comboBox1.SelectedValue;
catch { }
if(iCatID > 0)
var q = from p in ctx.Products
where p.CategoryID == iCatID
select p;
dataGridView1.DataSource = q.ToList();