Asp.net core ASP.NET核心Web API错误:模型1[TContext]违反了类型为';t上下文';
我在Visual Studio 2017中有一个包含以下项目的解决方案: CredentialManager.API(ASP.NET Core 2.1 Web API项目) 模型(包含域模型和数据上下文类的类库) 域模型类的编码如下所示:Asp.net core ASP.NET核心Web API错误:模型1[TContext]违反了类型为';t上下文';,asp.net-core,asp.net-core-webapi,Asp.net Core,Asp.net Core Webapi,我在Visual Studio 2017中有一个包含以下项目的解决方案: CredentialManager.API(ASP.NET Core 2.1 Web API项目) 模型(包含域模型和数据上下文类的类库) 域模型类的编码如下所示: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace CredentialManager.Mode
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; }
}
}
数据上下文类如下所示:
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; }
}
}
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";
});
}
这里有人能解释一下这个错误吗?如果我将类和数据上下文包含在与API项目相同的文件夹中,那么一切都正常。。但我希望这些类是一个单独的类库项目的一部分。任何帮助都将不胜感激
谢谢。更新上下文文件以具有以下内容:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"i.": null,
"CredentialManagerDB": "server=.\\SQLEXPRESS;database=CredentialManagerDB;Trusted_Connection=true;"
},
"AllowedHosts": "*"
}
public CredentialManagerContext(DbContextOptions<CredentialManagerContext> options)
: base(options)
{ }
public-CredentialManagerContext(DbContextOptions)
:基本(选项)
{ }
如文件所述:
这需要向DbContext类型添加一个构造函数参数,该参数接受:
DbContextOptions
这将解决您的问题。谢谢您的建议。我也找到了解决办法。需要将包含迁移的项目通知Startup.cs:
services.AddDbContext<CredManagerContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CredentialManagerDB"), x => x.MigrationsAssembly("CredManager.Models")));
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“CredentialManagerDB”),x=>x.migrationassembly(“CredManager.Models”);
在这之后一切都很顺利 您希望将哪些类移动到单独的项目中<代码>凭证和
凭证管理上下文
?检查项目中是否没有其他凭证
类是-凭证和凭证管理员上下文需要位于单独的类库项目中。项目中没有其他同名的类。我想您有一个成功构建的迁移吗?