Monday, April 1, 2019

Asp.net core project to migrate database scheme in a separate library project

For large asp.net core web project, the data access logic is usually put into a separate project, however, the database connection string is still configured in main project's appsettings.json file, so some extra settings is required when the data access project uses the connection string from main asp.net core project.
The data access library already added the below dependency
microsoft.EntityFrameworkCore
microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Sqlserver

1. update appsettings.json to include connection string
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
   "TravelInsuranceDb":  "Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=TravelInsurance;Integrated Security=True;"
  }
}


2. in main project's startup.cs, update configureServices method to AddDbContextPool, so that when needed, the custom DBContext object can be created by asp.net core to be used by application
    public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool<DataAccessDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("TravelInsuranceDb"));
            });
    

3. in data access library, create a DBContext sub class with a DBSet property
   public class DataAccessDbContext : DbContext
    {
        public DataAccessDbContext(DbContextOptions<DataAccessDbContext> options) : base(options)
        {
        }

        public DbSet<User> Users{get; set;}
    }
}
 public class User
    {
        [Key]
        public String Email { get; set; }  //this is the unique key for each user
    }

4. using dotnet migrations command line tool to create migration script. Whenever the entity structure is updated in the project, a new migration should be added. The -s parameter tells data access project where to find the connection string to connect to the database. After running the below command from data access library folder, the data access project should create the migration cs file for how to do the migration on database
dotnet ef migrations add initialcreate -s ..\travelinsurance\travelinsurance.csproj

5. using dotnet ef database command to update database, and verify the database is created
dotnet ef database update -s ..\travelinsurance\travelinsurance.csproj

6. Assume user entity needs a new int field "Point", first update the User class as below 
    public class User
    {
        [Key]
        public String Email { get; set; }  //this is the unique key for each user
        public int Point { get; set; }
    }
Then run the below command to add a new migration step script in the data access project.
dotnet ef migrations add initialcreate -s ..\travelinsurance\travelinsurance.csproj

7. run the database update command again to update the sql server, and verify the new column is added in the table.
dotnet ef database update -s ..\travelinsurance\travelinsurance.csproj


No comments:

Post a Comment