Sunday, November 16, 2025

EF Core - code first approach

 Create a Shared project with entities and install all required NuGet packages of Entity Framework


Creates Context file by extend class DbContext and user sql connection string as below

Then, In Visual Studio, open NuGet Package Manager Console from Tools -> NuGet Package Manager -> Package Manager Console and enter the following command

Make sure that Default Project points to where your context and entities are, and then execute the command, as shown below.

add-migration 'name of Migration name'


This will create a new folder named Migrations in the project and create the ModelSnapshot files, as shown below.


The Add-Migration command does not create the database. It just creates two snapshot files in the Migrations folder.

1) <timestamp>_<Migration Name>.cs: The main migration file which includes migration operations in the Up() and Down() methods. The Up() method includes the code for creating DB objects and the Down() method includes code for removing DB objects.

2) <contextclassname>ModelSnapshot.cs: A snapshot of your current model. This is used to determine what changed when creating the next migration.


Now, to create a database, use the update-database command in the Package Manager Console, as shown below.

Update-Database -verbose

The following executes the update-database command and creates the database, as shown below. The -verbose option shows the logs while creating the database. It creates a database with the name and location specified in the connection string in the UseSqlServer() method. It creates a table for each entity. It also creates the _EFMigrationHistory table that stores the history of migrations applied over time.

Apply Migrations for Modified Entities/Configurations

Suppose we add a new entity or modify an existing entity or change any configuration, then we again need to execute the add-migration and update-database commands to apply changes to the database schema.

Now, to sync our "Db" database with these changes, execute the following commands:

add-migration "ModifiedStudentEntity"

This will generate another snapshot in the Migrations folder.

Now, to update the database schema, execute the update-database command in PMC/PowerShell. This will add the columns in the Student table, as shown below.

In this way, you can keep adding, modifying or removing entities from EF Core model and sync the database using migrations.


Reverting Migration

For some reason, if you want to revert the database to any of the previous states then you can do it by using the update-database <migration-name> command.

For example, we modified the Student entity and added some more properties. But now we want to revert it back to the state of the "InitialSchoolDB" migration. We can do it by using the following command:

Update-database "InitialSchoolDB"

The above command will revert the database based on a migration named InitialSchoolDB and remove all the changes applied by the second migration ModifiedStudentEntity. This will also remove ModifiedStudentEntity entry from the __EFMigrationsHistory table in the database.










Monday, January 28, 2019

Adds a cookieless session ID to the virtual path

HttpResponse.ApplyAppPathModifier(String) Method



string urlConverted = Response.ApplyAppPathModifier("TestPage.aspx"); hlTest1.NavigateUrl = urlConverted;

Sunday, January 27, 2019

WebRequest authentication 401

Set location in web.config file to service url page. 

< location path="aspxpage.aspx">
< system.webServer>
        < security>
          < authentication>
            <anonymousauthentication enabled="true">
                  <windowsauthentication enabled="false">
                   </windowsauthentication>
            < /anonymousauthentication>
          </authentication>
        </security>
</system.webServer>

</location>

Monday, May 28, 2018

the field date must be a date in mvc

JQuery bootstrap date picker function in jquery.validate.js shows message 'the field date must be a date'. Override the validate date function from jquery.validate.js

Create jquery.validate.date.js and ensure it is loaded after jquery.validate.js

Reference from this link.

Add the following code to jquery.validate.date.js

$(function() {
    $.validator.methods.date = function (value, element) {
        if ($.browser.webkit) {

            //ES - Chrome does not use the locale when new Date objects instantiated:
            var d = new Date();

            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
        }
        else {

            return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
        }
    };

});

Thursday, April 24, 2014

Control of type 'GridView' must be placed inside a form tag with runat=server

As I thought, your code behind confirmed it that you indeed are exporting your grid.

The above exception occurs when one tries to export a GridView control to Word, Excel, PDF, CSV or any other formats. Here the .net compiler thinks that the control is not added to the form and is rendered, hence it throws this error even if your GridView control is inside a form with runat = “server”.

You have to tell the compiler that the control is rendered explicitly by overriding theVerifyRenderingInServerForm event.
Like:
public override void VerifyRenderingInServerForm(Control control)
{
   //
}

This will confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time

Friday, August 1, 2008

ASP.NET Querries

If you have any querries regarding asp.net, vb.net,c#.net and sql 2007 then please forward it to me. I'll be try to solve it.