Sunday, February 20, 2011

Sqlite plus Entity Framework 4

I have just started working with Sqlite, and i decided that it would be very interesting to use it together with Entity Framework. So lets just see how to do it:

The first step is to download Sqlite from http://sqlite.phxsoftware.com/ or the direct link http://sourceforge.net/projects/sqlite-dotnet2/files/SQLite%20for%20ADO.NET%202.0/1.0.66.0/SQLite-1.0.66.0-setup.exe/download .

This will install the provider to use with entity framework.

Then we have to had a entity model to our project:




And it is done, now we just have to use it:

using (var dataContext = new OurEntities())
{
   var statusList = from status in dataContext.Status
                        select status;
 
   foreach (var status in statusList)
   {
      Console.WriteLine(status.Name + " - " + status.Description);
   }
}

By the way, do not forget to add the connection string to your web.config or app.config:


<connectionStrings>
  <add name="OurEntities" 
connectionString="metadata=res://*/OurEntityModel.csdl|res://*/OurEntityModel.ssdl|res://*/OurEntityModel.msl;provider=System.Data.SQLite;provider connection string='data source=".\Database\OurDatabase.db"'" 
providerName="System.Data.EntityClient"   
connectionStrings>;

Notes: If you are using the version 4 of the framework you must add the following line to your web.config or app.config:

<startup useLegacyV2RuntimeActivationPolicy="true">;
    <supportedRuntime version="v4.0" startup;






References: http://dotnet.dzone.com/news/sqlite-entity-framework-4

Wednesday, February 16, 2011

My Blogs List

Feel free to suggest me more ;)

Code Snippets C# - Session is Null in a Ashx File

What to do if a session is null in a Ashx File?

Because a Ashx File is a HttpHandler, you will just need to implement the interface IRequiresSessionState and your problem is solved.

Reference: http://msdn.microsoft.com/en-us/magazine/cc164128.aspx

ASP.NET - Application Anatomy

Anatomy

One of the most remarkable features of the ASP.NET execution model, is the possibility to update the application without the need to restart the server and possible damage to users of the application. This means that adding, changing or removing files in the virtual directory can be done without any impact.
After making a change, the migration process to a new Application Domain is executed. This process executes a safe restart to the application and replaces the files in the temporary directory of ASP.NET.

Directories structure:

  • Bin - Contains all the application libraries (dll's)
  • APP_CODE - Contains all the classes that are dynamically compiled
  • APP_GLOBALRESOURCES - Contains global resources that are used by all application pages
  • APP_LOCALRESOURCES - Contains the resources used by the associated page
  • APP_WEBREFERENCES - Contains web services references
  • APP_DATA - Reserved to Data Storage such as SQL Express databases or xml files.
  • APP_BROWSERS - Contains xml files with browsers definitions, wich allow different renders between browsers
  • APP_THEMES - Contains the application themes

Global.asax

The file Global.asax,  allows you to write code that handles the global events of the application. The file is optional and is just allowed to have one per application and it must be in the root directory.

Application events:
  • Application_BeginRequest - Invoked in each Request
  • Application_EndRequest - Invoked at the end of each Request
  • Application_Start - Invoked when the application start
  • Session_Start - Invoked at the beginning of each session
  • Application_Error - Invoked when a not expected exception is thrown
  • Session_End - Invoked when a user session ends
  • Application_End - Invoked at the end of the application

Configurations

The configurations of an Asp.Net Application are made in a xml file. There are two types of configuration files, the global ones and the local, and each application can use more than one.

Global Configuration Files:

  • Location: C:\Windows\Microsoft.Net\Framework\[Version]\config
  • All the applications in the server will use that configuration file
Local Configuration Files
  • Location: The application root folder
  • Specific for the application
Config file elements
  •  responsible for the encription of the Asp.Net applications, session cookies and viewstate protection
  • where the connections to external systems are defined, such as database or active directory
  •  where you can define some of your application settings, like the quantity of data you want to retrieve for example
  •  this is one of the most important elements if not the most, and inside it you can set a lot of configurations such as:
    • authentication
    • compilation
    • customErrors
    • Membership
    • pages
    • profile
    • sessionstate
    • trace
There are a few more elements in the configuration file that are not important for this article.

The Page Class

All the Asp.Net pages inherit the base class Page. This is one of the most important classes in Asp.Net.
It contains 3 important properties:

  • Request represents the values and properties of the Http Request that originated the page loading
    • Cookies
    • Headers and ServerVariables
    • IsAuthenticated
    • IsLocal
    • QueryString
  • Response represents the server response to the client
    • Cookies
    • Redirect()
    • Write()
  • Server is a utilities class
    • MachineName
    • GetLastError()
    • HtmlEncode() and HtmlDecode()
    • MapPath()
    • Transfer

This article was translated from 
http://pplware.sapo.pt/pessoal/asp-net-%E2%80%93-anatomia-de-uma-aplicacao/