Following this, I made this stuff today:
- using Microsoft.EntityFrameworkCore;
using FluffyNothing.Core.Objects;
namespace FluffyNothing.Infrastructure.ExternalDependencies.Repositories
{
public class PlayerContext : DbContext
{
public PlayerContext(DbContextOptions options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder dbCob)
{
dbCob.UseSqlServer(ConnectionString.Use());
}
public DbSet<Player> Players { get; set; }
}
}
- using System.ComponentModel.DataAnnotations.Schema;
namespace FluffyNothing.Core.Objects
{
public class Player
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long PlayerId { get; set; }
public string PlayerIp { get; set; }
}
}
- using Microsoft.EntityFrameworkCore;
using FluffyNothing.Core.Interfaces.Repositories;
using FluffyNothing.Core.Objects;
namespace FluffyNothing.Infrastructure.ExternalDependencies.Repositories
{
public class PlayerRepository : IPlayerRepository
{
public void AddPlayer(Player player)
{
var optionsBuilder = new DbContextOptionsBuilder<PlayerContext>();
using (var context = new PlayerContext(optionsBuilder.Options))
{
//coming soon
}
}
}
}
At the NuGet console in Visual Studio 2019 I changed the "Default project:" to the infrastructure project and then I ran these two commands to make the database:
- Add-Migration EFCoreCodeFirstSample.Models.PaymentContext
- update-database
This sets up the database and storage for the migrations in a table at the database and a folder in the infrastructure project! When you cannot use .UseSqlServer with Microsoft.EntityFrameworkCore try this command:
Install-Package Microsoft.EntityFrameworkCore.SqlServer YourProjectNameHere
Addendum 7/20/2019: Add-Migration EFCoreCodeFirstSample.Models.PaymentContext above should really be Add-Migration EFCoreCodeFirstSample.Models.PlayerContext to tell the truth, you know? Another thing I should mention is that I had to have services.AddDbContext<PlayerContext>(); just inside ConfigureServices method in Startup.cs back in the UI project. Otherwise, the command I just mentioned won't run.
No comments:
Post a Comment