[英]Configuring Entity framework/DbContext
我已經苦苦掙扎了很長時間,無法將上下文類注入我的應用程序。
為什么我要創建的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; }
}
}
傑森計划:
{
"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,Version = v1.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
將此代碼添加到您的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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.