簡體   English   中英

在 EF Core 中添加遷移

[英]Adding Migration in EF Core

當我運行 Add-Migration 它給了我錯誤打擊:沒有為此 DbContext 配置數據庫提供程序。 可以通過覆蓋“DbContext.OnConfiguring”方法或在應用程序服務提供程序上使用“AddDbContext”來配置提供程序。 如果使用了“AddDbContext”,那么還要確保您的 DbContext 類型在其構造函數中接受 DbContextOptions 對象並將其傳遞給 DbContext 的基本構造函數。

這是我的代碼:

using Microsoft.EntityFrameworkCore;
using nftipy.Context;
using nftipy.Interface;
using nftipy.Repository;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

builder.Services.AddTransient<IUser, UserRepository>();
builder.Services.AddControllers();
builder.Services.AddDbContext<DatabaseContext>(option => 
    option.UseSqlServer("Data Source=.;Initial Catalog=NftipyDb;Integrated Security=true"));
builder.Services.AddEndpointsApiExplorer();


if (!app.Environment.IsDevelopment())
{
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();


app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html"); ;

app.Run();


using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using nftipy.Models;

namespace nftipy.Context
{
        public partial class DatabaseContext : IdentityDbContext<User>
        {
            public DatabaseContext()
            {
            }

            public DatabaseContext(DbContextOptions<DatabaseContext> options)
                : base(options)
            {
            }

            public virtual DbSet<User>? Users { get; set; }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>(entity =>
                {
                    entity.HasNoKey();
                    entity.ToTable("Users");
                    entity.Property(e => e.UserId).HasColumnName("UserId");
                    entity.Property(e => e.UserName).HasMaxLength(30).IsUnicode(false);
                    entity.Property(e => e.Email).HasMaxLength(50).IsUnicode(false);
                    entity.Property(e => e.Password).HasMaxLength(20).IsUnicode(false);
                });
                OnModelCreatingPartial(modelBuilder);

            modelBuilder.Entity<User>()
            .HasData(
                new User
                {
                    UserId = 1,
                    UserName = "Admin",
                    Email = "business.kam20i@gmail.com",
                    Password = "Admin1234"
                }
                );

            }

            

            partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
        }
    }

您可以添加設計時工廠。 這使您可以完全控制遷移創建期間上下文發生的情況,此外還可以完全控制構造函數。 我將此類存儲在與遷移相同的項目中。

 class MyContextDesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyContext>
{
    
    public MyContext CreateDbContext(string[] args)
    {
        
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
 
    optionsBuilder.UseSqlServer(config.GetConnectionString("MyConnString"));

        return new MyContext(optionsBuilder.Options);
    }}

請參閱此處了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM