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
Namoskar!!!
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)