我一直在努力将我的上下文类注入我的应用程序而没有任何成功。
为什么我创建的dbcontext类必须是通用类型?或者我做错了什么?谢谢。
Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Car_proj_new.Models;
using Microsoft.Data.Entity;
namespace Car_proj_new
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
var connection = Configuration["Data:ConnectionString"];
services.AddEntityFramework().AddDbContext<CarDbContext>( options => { options.UseSqlServer(connection); });
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Db上下文类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Car_proj_new.Models
{
public class CarDbContext: DbContext
{
public DbSet<Corporations> Corporations { get; set; }
public DbSet<PossibleFixes> PossibleFixes { get; set; }
public DbSet<Workers> Workers { get; set; }
}
}
Project.Json:
{
"dependencies": {
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"Areas/**/Views",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
错误:
错误CS0311类型'Car_proj_new.Models.CarDbContext'不能用作泛型类型或方法'EntityFrameworkServicesBuilder.AddDbContext(Action)'中的类型参数'TContext'。没有从'Car_proj_new.Models.CarDbContext'到'Microsoft.Data.Entity.DbContext'的隐式引用转换。 Car_proj_new..NETCoreApp,版本= 1.0
错误似乎很简单,但我无法理解,我很感激你的答案。
答案 0 :(得分:0)
这里没有代码,我可以说请检查以下内容
1)Name spaces
- 如果您的上下文类位于类库中,请检查项目是否已添加为引用(project.json
)
2)看起来您正在引用EF核心 - 我的 * guess * 但生成的上下文类来自或使用full .net framework
这是一个示例
using Microsoft.EntityFrameworkCore;
services.AddDbContext<MyContext>(options => { options.UseSqlServer(connection); });
{
"dependencies": {
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"dnxcore50",
"portable-net45+win8"
]
}
}
}
以上对我有用。技术堆栈.net core
和EF Core
答案 1 :(得分:0)
将此代码添加到您的CarDbContext中:
namespace Car_proj_new.Models
{
public class CarDbContext: DbContext
{
//***
public CarDbContext(DbContextOptions<CarDbContext> options) : base (options)
{
}
//***
public DbSet<Corporations> Corporations { get; set; }
public DbSet<PossibleFixes> PossibleFixes { get; set; }
public DbSet<Workers> Workers { get; set; }
}
}