ASP.NET Core Web API错误:模型1 [TContext]违反了“TContext”类型的约束

问题描述 投票:0回答:2

我在Visual Studio 2017中有一个包含以下项目的解决方案:

CredentialManager.API(ASP.NET Core 2.1 Web API项目)

CredentialManager.Models(包含域模型和数据上下文类的类库)

域模型类编码如下:

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

namespace CredentialManager.Models.Entities
{
    public class Credential
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long CredentialId { get; set; }

        [Required]
        public string Username { get; set; }

        [Required]
        public string Password { get; set; }

        [Required]
        public string Application { get; set; }
    }
}

数据上下文类如下:

using System;
using System.Collections.Generic;
using System.Text;
using CredentialManager.Models.Entities;
using Microsoft.EntityFrameworkCore;

namespace CredentialManager.Models.Context
{
    public class CredentialManagerContext : DbContext
    {

        public CredentialManagerContext(DbContextOptions options)
            : base(options)
        { }

        public DbSet<Credential> Credentials { get; set; }
    }
}

appsettings.json文件如下所示:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "i.": null,
    "CredentialManagerDB": "server=.\\SQLEXPRESS;database=CredentialManagerDB;Trusted_Connection=true;"
  },
  "AllowedHosts": "*"
}

Startup.CS文件如下所示:

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddDbContext<CredentialManagerContext>(o => o.UseSqlServer(Configuration["ConnectionStrings:CredentialManagerDB"]));

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }

然后我构建了解决方案并添加了迁移。但是当我运行update-database时,我收到以下错误:

GenericArguments[0], 'CredentialManager.Models.Migrations.CredentialManagerContext', on 'Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory`1[TContext]' violates the constraint of type 'TContext'.

这里有人可以对这个错误有所了解吗?如果我将类和数据上下文包含在与API项目相同的文件夹中,则一切正常。但我希望这些类成为单独的类库项目的一部分。任何帮助将非常感激。

谢谢。

asp.net-core asp.net-core-webapi
2个回答
1
投票

更新上下文文件以具有以下内容:

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

如文档中所述:

这需要在DbContext类型中添加一个构造函数参数,该类型接受:

DbContextOptions<TContext>

这应该可以解决您的问题。


0
投票

谢谢你提出的所有建议。我也找到了解决方案。需要通知Startup.cs包含迁移的项目:

services.AddDbContext<CredManagerContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CredentialManagerDB"), x => x.MigrationsAssembly("CredManager.Models")));

在此之后,一切都很完美。

© www.soinside.com 2019 - 2024. All rights reserved.