【问题标题】:Unable to create an object of type 'GameContext'无法创建“GameContext”类型的对象
【发布时间】:2021-06-06 12:38:51
【问题描述】:

我创建了一个 asp.net 核心项目,还创建了一个 ClassLibrary,并将它们加入到一个解决方案中。 我一步一步写我的方式:

1-dotnet new sln 2-dotnet new mvc -o GameShop 3-dotnet new classlib -o GameShop.Data 然后我在 GameShop.Data/Domain/User 中创建了一个类,我将其命名为“User.cs”

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GameShop.Data.Domain.User
{
    public class User
    {
          [Key]
        public int UserId { get; set; }
        [Display(Name = "نام کاربری")]
        [Required(ErrorMessage = "لطفا {0} را وارد کنید")]
        [MaxLength(200, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
        public string UserName { get; set; }
        [Display(Name = "ایمیل")]
        [Required(ErrorMessage = "لطفا {0} را وارد کنید")]
        [MaxLength(400, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
        [EmailAddress(ErrorMessage = "ایمیل وارد شده معتبر نمی باشد")]
        public string Email { get; set; }

        [Display(Name = "کلمه عبور")]
        [Required(ErrorMessage = "لطفا {0} را وارد کنید")]
        [MaxLength(200, ErrorMessage = "{0} نمی تواند بیشتر از {1} باشد")]
        public string Password { get; set; }

        [Display(Name = "تاریخ ثبت نام")]
        public DateTime RegisterDate { get; set; }
        [Display(Name = "کد فعال سازی")]
        public string ActiveCode { get; set; }

       

    }
}

这是我的创业公司:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
             services.AddDbContext<GameContext>(option =>
            {
                option.UseSqlServer(Configuration.GetConnectionString("GameShop"));
            });
        }

这是我的连接字符串:

{
  "ConnectionStrings": {
    "GameShop": "Data Source=.;Initial Catalog=GameShop-DB;Integrated Security=True"
  }

这是我的上下文,它的地址是:(GameShop.Data/Context/GameContext)

using System;
using Microsoft.EntityFrameworkCore;
using GameShop.Data.Domain.User;

namespace GameShop.Data.Context
{
    public class GameContext:DbContext
    {
        public GameContext(DbContextOptions<GameContext> option):base(option)
        {
            
        }
        public DbSet<User> Users { get; set; }
        
    }
}

那是我的代码。但是当我想“添加迁移”时,我有一个错误提示:无法创建“GameContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

这些是我在 GameShop.csproj 和 Gameshop.csproj 中都有的包:

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
  </ItemGroup>

(我在没有任何 ClassLibrary 的情况下尝试了这个(只有一个“asp.net core mvc 项目”)并且我没有任何错误)

那么谁能帮我做什么???

【问题讨论】:

    标签: c# visual-studio-code entity-framework-core asp.net-core-mvc entity-framework-migrations


    【解决方案1】:

    如果您从命令行运行“添加迁移”,则 GameShop.Data 项目无权访问 MVC 项目的 appsettings.json 文件中的连接字符串。

    我认为您需要在 Data 项目中实现 IDesignTimeDbContextFactory,以便迁移引擎可以连接到适当的数据库并确定目标数据库和模型之间的“差距”。比如:

    class GameShopDbContextFactory : IDesignTimeDbContextFactory<GameContext>
    {
       public GameShopDbContextFactory CreateDbContext(string[] args)
       {
          var builder = new DbContextOptionsBuilder<GameContext>();
          var connStr = {ADD YOUR CONNECTION STRING HERE}
          dbContextBuilder.UseSqlServer(connStr);
          return new GameContext(builder.Options);
       }
    }
    

    【讨论】:

    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 2021-08-15
    • 2021-03-07
    相关资源
    最近更新 更多