dotnet migration from command line

dotnet from command line can do more than just create projects and add packages. It can be used to work with ef core migrations. Lets start by creating a new console app. Run this command: dotnet new console. Add these three packages:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Your csproj should now look like this:


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
  </ItemGroup>

</Project>

Open the project and add a Student class:
public class Student{
    public System.Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Grade { get; set; }
}

Add these two classes:
    namespace efcoretest
    {
        public class SchoolDbContext : DbContext
        {
            public SchoolDbContext(DbContextOptions<SchoolDbContext> options) : base(options)
            {
            }
            public DbSet<Student> Students { get; set; }
        }
 
 
        public class DBContextFactory : IDesignTimeDbContextFactory<SchoolDbContext>
        {
            public SchoolDbContext CreateDbContext(string[] args)
            {
                var optionsBuilder = new DbContextOptionsBuilder<SchoolDbContext>();
                optionsBuilder.UseSqlServer("Data Source=(localdb)\\testdb; Initial Catalog=SchoolDb; Integrated Security=True");
 
                return new SchoolDbContext(optionsBuilder.Options);
            }
        }
    }

Go to the command line and type this command: dotnet ef dbcontext info. You should get this result:

Provider name: Microsoft.EntityFrameworkCore.SqlServer
Database name: SchoolDb
Data source: (localdb)\testdb
Options: None

You are now ready to use migrations. Run this command: dotnet ef migrations add initialcreate. If you check your project, you will notice a new folder called Migrations has been created. In that folder you will find a class that contains c# code to create a Students table. To create the database, run this command: dotnet ef database update. Checking SSMS will show the following:


And finally, right some code to test insertion:
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        var optionsBuilder = new DbContextOptionsBuilder<SchoolDbContext>();
        optionsBuilder.UseSqlServer("Data Source=(localdb)\\testdb; Initial Catalog=SchoolDb; Integrated Security=True");
 
        using (var context = new SchoolDbContext(optionsBuilder.Options))
        {
            var student = new Student{  Id=Guid.NewGuid(), FirstName="Joh"};
            context.Add(student);
            context.SaveChanges();
 
        }
    }

Comments

Popular posts from this blog

Adding CKEditor 4 to Angular

Using @CurrentIteration with TFS API