Monday, October 19, 2009

Visual Studio 2010 and .NET Framework 4 Beta 2

 

For MSDN subscribers VS10 .NET framework 4 Beta 2 is available on this wednesday .

Download it from here

Sunday, October 18, 2009

An anonymous type cannot have multiple properties with the same name

If the query is like this

adventureworksEntities dbEntities = new adventureworksEntities();
            var query = from PC in dbEntities.productcategory
                        join PSC in dbEntities.productsubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PC.Name, PC.ModifiedDate, PSC.Name, PSC.ModifiedDate };

The compiler will throw the error,”An anonymous type cannot have multiple properties with the same name” .Because the compiler can’t able to find a proper Name.So we want to modify the query with alias name ,some thing like this.



  var query = from PC in dbEntities.productcategory
                        join PSC in dbEntities.productsubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PCName1 = PC.Name, PCModifiedDate1 = PC.ModifiedDate, PSCName1 = PSC.Name, PSCModifiedDate1 = PSC.ModifiedDate };

Entity Framework for MSSQL and MYSQL

As we all know that Entity Frame work supports multiple databases.Here i am converting MSSQL database to MYSQL,Without changing the query.That is the power of linq.

First i am doing one sample on MSSQL.

I am using AdventureWorks Database, and using 2 tables ,Production.ProductCategory and Production.ProductSubcategory.

The ER Diagram for Production.ProductCategory and Production.ProductSubcategory is shown below

ED1

Steps to Add Entity Frame Work for MSSQL

Step 1 : Right Click on Solution Explorer –> Add –> New Item –> Select ADO.NET Entity Data Model.

MS1

Step 2 : Select sever name and AdventureWorks as DataBase and Click ok

MS2

Step 3 : Click Next

MS3

Step 4 : Select two tables Production.ProductCategory and Production.ProductSubcategory and Click Finish

MS4

So your Edmx Looks like

MS5

Code for getting the data

 AdventureWorksEntities dbEntities = new AdventureWorksEntities();
            var query = from PC in dbEntities.ProductCategory
                        join PSC in dbEntities.ProductSubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PCName = PC.Name, PCModifiedDate = PC.ModifiedDate, PSCName = PSC.Name, PSCModifiedDate = PSC.ModifiedDate };
            GridView1.DataSource = query;
            GridView1.DataBind();

Here i cannot use Debug Visualizer.Entity Frame work wont support Debug Visualizer.So i am using one gridview and dispaly the data.

Note : If you are getting the error “An anonymous type cannot have multiple properties with the same name Read this

Output shown in Gridview.

MS6

The same result i am getting from MYSQL without changing the query.Only i am changing the database from MSSQL to MYSQL.

Converting MSSQL to MYSQL by using Full Convert Enterprise .

Steps to Covert

Step 1 : Already created AdventureWorks DataBase in MYSQL.

FC1


Step 2 : Enter username and Password

FC2

Step 3 : I selected all tables selected to convert MYSQL.

FC3

Step 4 :Click Convert.

FC4

Step 5 : Click Continue.

FC5

After Convert It shows a result like

FC6

Steps to Add Entity Frame Work for MYSQL.

Step 1 : install Connector/ NET 6.0 Windows Binaries,it automatically install mysql.data.dll and mysql.data.entity.dll to GAC.

Step 2 : Right Click on Solution Explorer –> Add –> New Item –> Select ADO.NET Entity Data Model.

MY1

Step 3 : Select New Connection.

MY2

Step 4 : Change the Data Source from MSSQL to MYSQL.

MY3

Step 5 : Select MySQL .if you install Connector/ NET 6.0 Windows Binaries ,only it will come.

MY4

Step 6 : Enter the necessary Details and click ok.

MY5

Step 7 : Select the necessary fields and click next.

MY6

Step 8 : Select two tables Production.ProductCategory and Production.ProductSubcategory.

MY7

The edmx will be same as previous MSSQL edmx.

MY8

Use the same query in MSSQL .

 adventureworksEntities dbEntities = new adventureworksEntities();
            var query = from PC in dbEntities.productcategory
                        join PSC in dbEntities.productsubcategory
                      on PC.ProductCategoryID equals PSC.ProductSubcategoryID
                        select new { PCName = PC.Name, PCModifiedDate = PC.ModifiedDate, PSCName = PSC.Name, PSCModifiedDate = PSC.ModifiedDate };
            GridView1.DataSource = query;
            GridView1.DataBind();
The result will display as same result.

1

Note it is easy to convert from one database to another database. But still some errors are there in EDMX Framework ( it wont support some joins ,Math operations etc).All the bugs will overcome on .NET 4.0 . Wait and see.

Friday, October 16, 2009

ALTER ALL TABLE,Procedure and UDF SCHEMA IN A SINGLE QUERY

 

By using this query we can alter the schema to SCHEMANAME .

select 'ALTER SCHEMA SCHEMANAME transfer ['+SCHEMA_NAME(schema_id)+'].['+name +']' from sys.tables

OUTPUT

schema

select all the query and execute in query window.All the schema will automatically changes to SCHEMANAME

Note :sys.tables – Returns a row for each table object ,currently only with

sys.objects.type =U

SCHEMA_NAME(schema_id) – Returns the schema name associated with schema_id

schema_id – ID of the schema.Schema_id is a int. If schema is not defined schema name of default will return

E.g : select SCHEMA_NAME() from sys.tables

OUTPUT

dbo

For Procedure

select 'ALTER SCHEMA SCHEMANAME transfer ['+SCHEMA_NAME(schema_id)+'].['+name +']' from sys.procedures

For UDF

select 'ALTER SCHEMA SCHEMANAME transfer ['+specific_schema+'].['+specific_name +']'
FROM information_schema.routines
WHERE routine_type='function'

Note : routines - Returns one row for each stored procedure and function that can be accessed by the current user in the current database.

Wednesday, October 7, 2009

NDepend – Part 1

Its a .NET Static analysis tool for Architecture and developers to analyze the code structure,specify design rules , do some massive code review , compare different versions of code etc . This tool is developed by Patrick Smacchia(C# MVP).

Main features of NDepend are as follows as

  1. Code Query Language(CQL)
  2. Compare Builds
  3. 82 Code metrics
  4. Manage Complexity and Dependencies
  5. Detect Dependency Cycles
  6. Harness Test Coverage Data
  7. Enforce Immutability and purity
  8. Warning about the health of your Build Process
  9. Generate custom report from your Build Process
  10. Diagrams
  11. Facilities to cope with real-world environment

To read more

This is the sample program i am going to analyze by using NDepend.

using System;
using System.IO;
using System.Collections.Generic;
interface IFirst
{
    int AgeFirst { get; set; }
    string NameFirst { get; set; }
}
interface ISecond
{
    int AgeSecond { get; set; }
    string NameSecond { get; set; }
}
abstract class abstractClass
{
    int Ageabstract { get; set; }
    string Nameabstract { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
    }
}

As we all know that the interface is not derived from System.Object.

and the abstract is derived from System.Object.

The Dependency Matrix in NDepend helps to See things graphically.

interface VS Abstract1


For IL Instruction for abstract class can be find out with the help of Metrics in NDepend as follows.

Abstract1

There are lot of things like Code Query Language(CQL), Detect Dependency Cycles,Compare Builds etc are there to explore in NDepend tool.That i will cover the the upcoming parts.

If any one interested in this tool, have a look on NDepend and order it Now.

Note : Thanks Patrick Smacchia, for given a free licence of NDepend to write a review.

Thursday, October 1, 2009

New MVPs Announced – October 2009

Microsoft MVPs are awarded every quarter and recognise the contributions of an individual to technical communities over the past year. read more

 
Counter