我正在使用实体框架 7 并创建了两个项目。一个项目是 ASP.NET 5 Web API 项目,另一个是类库项目(包),我想在其中存储所有数据访问层逻辑。这样我就可以将此包用于另一个报告项目,并且我可能提供的其他附加服务。
基本上,我在 Web api 项目的控制器中有一个简单的帖子,它调用我的数据库项目中的函数。当函数启动数据库时,它说数据库未定义,即使它在两个项目中都定义了。
ERROR
{"No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services."}
CODE
数据库项目:InsertPerson
public int InsertPerson(tbl_Person person)
{
using (var db = new AppContext())
{
try
{
db.tbl_Person.Add(person);
db.SaveChanges();
return person.PersonID;
}
catch
{
return 0;
}
}
}
配置文件
Web API 项目:Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<AppContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppContext>();
services.AddMvc();
}
Web API 项目:appsettings.json
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=localhost;Database=testDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
}
}
Web API 项目:project.json
{
"userSecretsId": "blah,
"version": "1.0.0-rc1-final",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"DataLibrary": "",
"Microsoft.AspNet.Authentication.OAuthBearer": "1.0.0-beta7",
"Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
"Remotion.Linq": "2.0.1"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
数据库项目:Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<AppContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppContext>();
services.AddMvc();
}
数据库项目:appsettings.json
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=localhost;Database=testDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
}
}
数据库项目:AppContext.cs
namespace DataLibrary
{
public class AppContext : IdentityDbContext<ApplicationUser>
{
public DbSet<tbl_Person> tbl_Person { get; set; }
public static AppContext Create()
{
return new AppContext();
}
protected override void OnModelCreating(ModelBuilder builder)
{
new tbl_PersonMap(builder.Entity<tbl_Person>());
base.OnModelCreating(builder);
}
}
}