Sunday, May 31, 2015

Installing/Updating Entity Framework


Open a New project of your choice(MVC,Web Application, Console Application.) in visual studio. I have taken MVC4 Internet Application as example. The following procedure and the implementation explained later applies for all project types.

Right Click on the project in solution explorer and select "Manage NuGet Packages".


Click "Install" or "Update" against "Entity Framework".



Once done, Entity FrameWork will be installed/updated and added to your project reference.

Friday, May 29, 2015

Entity FrameWork Code First - Introduction


Code First approach was introduced with Entity Framework 4.1. With code first, you can start building your classes based on your business requirement first and entity framework will take care of creating the databases and tables. (If you don't want your databases to be automatically created, you can turn off this feature).

Why Code First ?

Though Database First/Model First approaches seem to be effortless at the beginning, it becomes difficult when you want to have more and more customization to your properties/database columns. With these approaches, .edmx model files will be auto-generated. Yes, it seems to have taken care of most of your job. But you can never alter these files, because any changes that you make will be lost when you regenerate the model files again for some reason.(eg. adding a column).  For example, if you want to have a different display name for a column (rather than the one in the database), you will have to create a partial class and extend your model class. You have to do this for almost every model that you are creating.

But with code first approach, you have more control over customizing your columns(making it a required field/read only etc..) as the model that you hand code becomes your database. You can precisely and easily specify the relationship and the constraints. Most of all, Entity Framework provides an excellent version control of your database which will otherwise be cumbersome.

When you are planning to create a small project, you can very well use database/model first approaches as it will greatly reduce your time. But for mediocre to big projects, code first is always a recommended approach.

Next>> Installing/Updating Entity Framework

Wednesday, May 27, 2015

Changing DefaultConnection to Sql Server DB in Auto generated MVC code

When "Internet Application" is chosen as template for MVC4 Web Application, we can see the auto generated code for the home page including user login logic. The tables will be created in the local DB instance in Visual studio in MDF file format as shown below.
But if you wish to store the data in your database in sql server, you have to make the following modifications.

1. Add your connection string in web.config file.

  <connectionstrings>

    <!-- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Triczz-20150512151642;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-xxxx.mdf" providerName="System.Data.SqlClient" /> -->


   <add name="SomeConnectionName" connectionString="Data Source=.\SQLEXPRESS; Database=YourDB; Integrated Security=SSPI" providerName="System.Data.SqlClient" />


  </connectionStrings>


  
You can remove the "DefaultConnection" string commented above,

2. In AccountModels.cs, UsersContext uses "DefaultConnection".


Change "DefaultConnection" above to "YourDB" that you gave in web.config file. Or you can even pass the name of the connection string as :base(name="SomeConnectionName")

3. In WebSecurity.InitializeDatabaseConnection the connection name should be changed. It can be found in Global.asax or InitializeSimpleMembershipAttribute.cs in Filters folder.

Change the "DefaultConnection" to "SomeConnectionName" that you gave in web.config file.


Once the above changes are made, run the project to see the "UserProfile" and the other tables being created in the specified database.

History of C#


1-C# History