Home > other >  .NET 6 uses EF CORE to migrate
.NET 6 uses EF CORE to migrate

Time:11-20

I have upgraded the .NET version to the latest 6.0, but I cannot use EF Core to migrate. The change of the version makes me feel very unfamiliar, and the information is relatively small. Any information or help you have is great!

CodePudding user response:

What is the problem that you cannot use EF Core for migration? For the .net6 version, the migration changes are not very big. I wrote a simple demo based on some materials, I hope it will be helpful to you.

csproj file:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>

Then add the database connection configuration in appsettings.json:

"ConnectionStrings": {
    "DefaultConnection": "Your Db"
  }

Then in the .net6 version, there is no Startup.cs configuration class, and some configuration is done in Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();


var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<UserContext>(options => options.UseSqlServer(connectionString));

Model:

 public class User
    {

        [Key]
        public int Id { get; set; }

        public string Name { get; set; }

       public DateTime CreatedDate { get; set; }
      
    }

Create context:

 public class UserContext:DbContext
    {
        public UserContext(DbContextOptions<UserContext> options) : base(options) { }
        public DbSet<User> Users { get; set; }
    }

Then use the migration command:

add-migration MigrationName
update-database

Test:

 public class TestController : Controller
    {
        private readonly UserContext _context;

        public TestController(UserContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            User user = new User();
            user.Name ="Test";
            user.CreatedDate = DateTime.Now;
               _context.Add(user);
              _context.SaveChanges();                  
            return View();
        }
    }

Result:

enter image description here

I use Code first migration as an example. If you follow the steps and have problems, you can send out the error, or you can send out the error you are encountering now, and you can read some changes in .net6:

https://gist.github.com/davidfowl/0e0372c3c1d895c3ce195ba983b1e03d

  • Related