Posted in .Net Basics, ADO.Net, C# 4.0, Microsoft

Entity Framework 5 is released

Microsoft has just release a latest version of Entity Framework, version 5. EF5 is available as the Entity Framework NuGet package and is also included in Visual Studio 2012.

below is the some useful information from ADO.Net Blog

What’s New in EF5

EF 5 includes a number of new features and bug fixes to the EF4.3 release. Most of the new features are only available in applications targeting .NET 4.5, see the Compatibility section for more details.

  • Enum support allows you to have enum properties in your entity classes.
  • Spatial data types can now be exposed in your model using the DbGeography and DbGeometry types.
  • The Performance enhancements that we recently blogged about.
  • Code First will now detect if you have LocalDb or SQL Express available for creating new databases. Visual Studio 2012 includes LocalDb, whereas Visual Studio 2010 includes SQL Express.
  • Code First will add tables to existing database if the target database doesn’t contain any of the tables from the model.

The EF Designer in Visual Studio 2012 also has some new features:

  • DbContext code generation for new models means that any new models created using the EF Designer will generate a derived DbContext and POCO classes by default. You can always revert to ObjectContext code generation if needed. Existing models will not automatically change to DbContext code generation.
  • Multiple-diagrams per model allows you to have several diagrams that visualize subsections of your overall model. Shapes on the design surface can also have coloring applied.
  • Table-Valued functions in an existing database can now be added to your model.
  • Batch import of stored procedures allows multiple stored procedures to be added to the model during model creation.

Get Started @ msdn.com/data/ef

The updated EF MSDN site includes a bunch of walkthroughs and videos to get you started with Entity Framework, including the new features in EF5.

What Changed Since the Release Candidate

You can see a list of bugs that we fixed since the EF5 RC on the EF CodePlex site.

The most notable change since RC is that new models created using the EF Designer in Visual Studio 2012 will now generate a derived DbContext and POCO classes by default. You can always revert to ObjectContext code generation if needed.

Compatibility

This version of the NuGet package is fully compatible with Visual Studio 2010 and Visual Studio 2012 and can be used for applications targeting .NET 4.0 and 4.5.

Some features are only available when writing an application that targets .NET 4.5. This includes enum support, spatial data types, table-valued functions and the performance improvements.

Need Help

Head to the EF MSDN site to find out how to get help with Entity Framework.

Thank you Entity Framework Team for this update..

 

 

Advertisements
Posted in .Net Basics, C# 4.0

Memory management in Microsoft dotnet (.Net)

Hello everyone,
Today I came across a good resource on memory management in Microsoft .Net, so I would like to share this with all..

on website of Red-gate I found some videos, click here to see them.

Posted in .Net Basics, C# 4.0

Abstract Classes in .Net

An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

Its a special type of class that cannot be instantiated. (So why we need that ??) There are some reasons, an Abstract class is only to be sub-classed. That means it enforces some contracts or say methods to all the subclasses.

Here is a basic sample of abstract class and how to access their abstract and non abstract methods.

abstract class testDemo
    {
        public void callme() // non abstract method
        {
            // Some implementation
        }

        abstract public void ImplementMe(); // abstract method
    }

    class Child : testDemo
    {
        //implement the abstract method
        public override void ImplementMe()
        {
            throw new NotImplementedException();
        }
    }

This is how you can access methods of the abstract class.

 public void TestMethod()
        {
            Child obj = new Child();
            obj.callme();       // call to a non abstract method
            obj.ImplementMe();  // call to an abstract method which is implemented on the Child class
        }

An abstract class can also have abstract properties as well, derived class has to implement this property.

An Abstract class cannot be a sealed class. Following is not valid

  abstract sealed class testDemo {}

An abstract Method cannot be Private. Access modifier of the abstract method must be the same in both abstract class and its derived class. Otherwise it will be detected as error by compiler.

private abstract int myProp(); // incorrect

An Abstract method can’t be virtual as it is implicitly virtual.

An abstract member can’t be static but an abstract method can be a static one.

Hope this help someone.

Posted in C# 4.0, Silverlight

Dynamic Style/theme Change in Silverlight 4.0

In Silverlight application, we often need to change the theme or style dynamically. It is quite simple to do from code side. Here is an example how to do that.

I have two separate theme files, Theme1.xaml and Theme2.xaml defined as ResourceDictionary.

Theme1.xaml

  <ResourceDictionary  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
  xmlns:sys="clr-namespace:System;assembly=mscorlib">       
  <!-- explicit style (has a key) -->
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">
        <Setter Property="Text" Value="SuperAwesome Co. LLC" />
        <Setter Property="Foreground" Value="Orange" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="15" />
 </Style>
 <!-- implicit style (no key - just a TargetType) -->
 <Style TargetType="Button">
     <Setter Property="Foreground" Value="Orange" />
     <Setter Property="Background" Value="Yellow" />
</Style>
</ResourceDictionary>

Theme2.xaml

<ResourceDictionary     
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
   xmlns:sys="clr-namespace:System;assembly=mscorlib">     
   <!-- explicit style (has a key) -->     
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">         
     <Setter Property="Text" Value="SomeOtherCompany Inc." />         
     <Setter Property="Foreground" Value="CornflowerBlue" />        
     <Setter Property="FontWeight" Value="Bold" />         
     <Setter Property="FontSize" Value="14" />     
  </Style>       
  <!-- implicit style (no key - just a TargetType) -->     
  <Style TargetType="Button">         
     <Setter Property="Foreground" Value="BlueViolet" />         
     <Setter Property="Background" Value="DarkOrchid" />     
  </Style> 
</ResourceDictionary>

Now I have added their reference in my App.xaml as below;

 <Application.Resources>         
    <ResourceDictionary>             
       <ResourceDictionary.MergedDictionaries>                 
         <ResourceDictionary Source="Themes/theme1.xaml"/>                 
         <ResourceDictionary Source="Themes/theme2.xaml"/>             
       <ResourceDictionary.MergedDictionaries>         
   </ResourceDictionary>    
</Application.Resources>

By default it will use Theme2.xaml as it is the last defined style. Now we’ll change the theme and use Theme1.xaml. Here is the code to change the theme dynamically from code behind.

  ResourceDictionary rd = new ResourceDictionary();
  //Load resource dictionary
  rd.Source = new Uri("/Themes/theme1.xaml", UriKind.RelativeOrAbsolute);
  //Clear previous styles if any...
  App.Current.Resources.MergedDictionaries.Clear();
  //Add the loaded resource dictionary to the application merged dictionaries
  App.Current.Resources.MergedDictionaries.Add(rd);

Hope this help some one 🙂

Posted in C# 4.0, Silverlight, WPF

Load Content of a Window object to another Control

Recently I have been working with WPF. I was developing a functionality of Child window and I need to load the content of existing windows to that child window control. I got the Child Window control from the CodePlex. I think many of you must be aware of it. There is a project called Extended WPF toolkit. You can find some nice control there. Here is the code to load the content of Window object to a user control (Child Window in my case).

Window2 win = new Window2();
Grid newContent = win.Content as Grid;
win.Content = null;
ChildWin.Content = newContent;

“Window2” is the name of my window that I wanted to open in child window. The root element in the Window2 is Grid, so I cast the content as Grid and assigned it to a local variable of type Grid.

Now, note the use of: win.Content = null;

To use the content of an object we need to disconnect the content from its parent, in this case it is the instance of Window2. This line will do the trick and allow you to use the content the way you like. I have put this content to my child window’s content and that is it.

I got what I want.

You can see above, I have display the content of Window2 to my Child Window control.

I hope this help to someone.

Posted in C# 4.0, SQL Server

SQL Server Tips – Find # of business days between two dates

To find the business days or say working days between two dates. This could be the easiest task for many but I would still like to make a not of this, so I am writing this post.

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
DECLARE @IsSaturWorking bit
SET @StartDate ='2011/07/01'
SET @EndDate ='2011/07/31'

SELECT  (DATEDIFF(dd, @StartDate, @EndDate)+ 1)  -
 (DATEDIFF(wk, @StartDate, @EndDate)* 2)  -
        (CASEWHENDATENAME(dw, @StartDate)='Sunday'THEN 1 ELSE 0 END)-
        (CASEWHENDATENAME(dw, @EndDate)='Saturday'THEN 1 ELSE 0 END)

I hope this help someone.


	
Posted in .Net Basics, C# 4.0

yield in C#

yield – another useful tool in C#. This keyword is used in a combination with return.

What it does basically ??

It is helpful in the iterations like for and foreach. You can only use this on enumerators, such as one that returns IEnumerable. You can use it to “save the state” of the function after the return. After the yield is reached, and the value is returned, the foreach loop will call the function again, but you will pick up right where you left off. Let me show you this with an example:

public class MyList
{
    //using System.Collections;
    public static IEnumerable<int> GetPower(int number, int exponent)
    {
        int counter = 0;
        int result = 1;
        while (counter++ < exponent)
        {
            result = result * number;
            yield return result;
        }
    }

    static void Main()
    {
        // Display powers of 2 up to the exponent 9:
        foreach (int i in GetPower(2, 9))
        {
            Console.Write("{0} ", i);
        }
    }
}
/*
Output:
2 4 8 16 32 64 128 256 512
*/

Please note the use of generic and define the return type as enumerable of int type, this performs well as it avoid boxing and unboxing of the object the loop is returning. If you use only IEnumerable it will do box-unbox at the time of returning the element.

The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement.

Practically useful ??

Yes, you can use yield in several ways. For example, you can use it as a filter for displaying random HTML code in your ASP.NET website. Internally, you can select random elements from an array and yield them, such as for advertisements.

It also used with the “break” keyword.

I hope this help someone.

For more details yields in C# Reference on MSDN.

Posted in .Net Basics, C# 4.0

dynamic type in C# 4.0

The type dynamic was introduced with C# 4.0 long back but I have used this very recently in one of my project, so just want to share my experience with this wonderful feature of C# 4.0.

dynamic

The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. It behaves like type object in most circumstances, however the operations that contain expressions of type dynamic are not resolved or type checked by the compiler.

Variable of type dynamic is compiled into variable of type object, and that is why the dynamic type is exists at compile time but not at runtime.

How to use dynamic – a simple example:

class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

output:

System.Int32
System.Int32

you will see the output returns the same type as both the variables are having int value assigned. If you want to see the difference between dyn and obj at compile time, try following lines and compile the code.

dyn = dyn + 3;
obj = obj + 3;

The compiler raised an error for attempted addition of an integer and an object in expression obj + 3. But, no error is reported for dyn + 3. The expression that contains dyn is not checked at compile time because the type of dyn is dynamic.

For more information about the dynamic type http://msdn.microsoft.com/en-us/library/dd264741(VS.100).aspx and Using Type dynamic