Showing posts with label CabFile. Show all posts
Showing posts with label CabFile. Show all posts

Feb 5, 2015

Code First Example for Enitty Frame work

Entity Framework is an enhancement to an existing ADO.Net technique and ORM framework we used for Microsoft technologies.

There are the following 3 approaches we use during application development:
  1. Code First
  2. Model First
  3. Database First (DB first)
So, in this article we will learn how to develop a simple C# application using the Entity Framework code first approach.

You need Visual Studio 2010/2012/2013 and SQL Server to learn this.

Create a new C# Class Library project named "CodeFirstLibrary". Add a new class named "Employee" to the project.

Employee Class:



  public class Employee 
    {
        public Employee ()
        { 
        
        } 
        public int EmployeeID { get; set; } 
        public string Name { get; set; } 
    }  

Add a new class derived from the DBContext class with a DbSet property for the Employee class.

References needed: System.Data.Entity and EntityFramework

public class Context: DbContext  
 { 
     public Context(): base() 
     { 
  
     } 
     public DbSet<Employee> Employees { get; set; } 
 }  

 
Add a new console application project named "CodeFirstConsole" to this solution so that we can test these classes:

using (var ctx = new Context()) 

    Employee emp = new Employee() { name = "Prashant" }; 
    ctx.Employees.Add(emp); 
    ctx.SaveChanges();                 
}  
  

Set "CodeFirstConsole" as the startup project. Run the application and see what is happening. It has successfully stored the Employee information into the database.

Surprised?

Where is the database? How was it created? What are the tables created, what is the table structure?

This is the beauty of Code First for the Entity Framework. It creates the database based on a parameter passed in the base constructor of your context class. We have not passed any parameters so it will create a “CodeFirstLibrary.Context” database in the local SQLEXPRESS.

Some Important points to note here are:
  1. Code-first will create a table in the database. The "Employees’ table is created based on the Employee class.

  2. Code First APIs create a PrimaryKey in the table if the class has either an “Id” or ClassName + “Id” property. For example, the Employee class has the “EmployeeID” property so it will create EmployeeID as the PK column.

  3. It will also create other columns with the same name and datatype as property names as shown in the following screenshot.
So, using EF Code First, we can create applications that will generate a DB at runtime.
 

Jul 5, 2011

Creating a .cab file with C#


It is Microsoft DTF that comes with Wix 3.0  Download wix3.0 and Install it, then you will have new dll  Microsoft.Deployment.Compression and Microsoft.Deployment.Compression.Cab
 in Project>>Add Refernce>>.net tab, add reference to those dll 's in your application .
In your code"Microsoft.Deployment.Compression.CompressionLevel.Max, EventHandler);" , it is not logic here not only because it's not a legal sentence expecially lack of '(', also it's to Pack files, not Unpack.
//Pack files should be like this:
CabInfo cab = new CabInfo(@"C:\Cabinet2.cab");
cab.Pack(@"C:\folder", true, Microsoft.Deployment.Compression.CompressionLevel.Max, null);
//Unpack a cab file into C:\Unpacked folder :cab.Unpack(@"C:\Unpacked");   

or



At present we do not have classes in .NET that enable us to create CAB (cabinet) files, hence we has other third party libraries and toolsets that provide their custom classes, one such is Windows Installer XML (WiX) toolset (http://wix.sourceforge.net) which is distributed with a collection of dlls as part of its SDK and best of all is that this is developed by Microsoft and we have good support for WiX users.

In your .NET project add references to the following DLLs:
Microsoft.Deployment.Compression.dll
Microsoft.Deployment.Compression.Cab.dll
And we will be using this library (Microsoft.Deployment.Compression.Cab) for creating CAB (cabinet) files.


using Microsoft.Deployment.Compression.Cab;


In our example we will see how to add individual files to the CAB file and how to add a folder (directory) to the CAB file and finally how to extract (unpack) the CAB file.





// CREATING CAB FILE BY ADDING LIST OF FILES
CabInfo cab = new CabInfo(@"C:\testarchive1.cab");
List files = new List();
files.Add(@"C:\test1.txt");
files.Add(@"C:\test2.txt");
files.Add(@"C:\test3.txt");
cab.PackFiles(null,files,null);

// CREATING CAB FILE BY ADDING FOLDER (WITH SUB-FOLDERS) USING MINIMUM COMPRESSION
cab = new CabInfo(@"C:\testarchive2.cab");
cab.Pack(@"C:\Balaji", true, Microsoft.Deployment.Compression.CompressionLevel.Min, null);

// EXTRACTING (UNPACKING) FILES FROM CAB FILE
cab = new CabInfo(@"C:\testarchive1.cab");
cab.Unpack(@"C:\ArchieveDir");

the location for dlls after installation is

C:\Program Files\Windows Installer XML v3.5\SDK



Another dll that also comes with WiX toolset SDK isMicrosoft.Deployment.Compression.Zip.dll that provides similar functionality for packing and unpacking zip files.