你应该remove "EntityFramework"
从依赖项中删除行"EntityFramework": "7.0.0-beta4"
)并使用
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final"
仅有的。你不需要添加"EntityFramework.Core": "7.0.0-rc1-final"
如果您明确添加"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
由于以下依赖关系:
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final":
"EntityFramework.Relational": "7.0.0-rc1-final":
"EntityFramework.Core": "7.0.0-rc1-final"
我的意思是EntityFramework.MicrosoftSqlServer
需要EntityFramework.Relational
,这需要EntityFramework.Core
.
您还应该验证global.json
包含"sdk": { "version": "1.0.0-rc1-update1" }
.
UPDATED: 我检查了你上传的测试项目。它有很多小问题。我修改了它。可以下载修改后的项目here. 不要忘记修复ConnectionString
用于appsettings.json
,它包含Server
我对我用过。
最重要的变化是使用更简单BookContext.cs
:
using Microsoft.Data.Entity;
namespace apiservice.Models {
public class BookContext : DbContext {
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
}
}
以下的用法SampleData.cs
using System;
using System.Linq;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
namespace apiservice.Models
{
public static class SampleData
{
public static void Initialize(IServiceProvider serviceProvider) {
var context=serviceProvider.GetService<BookContext>();
context.Database.Migrate();
if (!context.Books.Any()) {
var austen = context.Authors.Add(
new Author { LastName = "Austen", FirstName = "Jane" }).Entity;
var dickens = context.Authors.Add(
new Author { LastName = "Dickens", FirstName = "Charles" }).Entity;
var cervantes = context.Authors.Add(
new Author { LastName = "Cervantes", FirstName = "Miguel" }).Entity;
context.Books.AddRange(
new Book {
Title = "Pride and Prejudice",
Year = 1813,
Author = austen,
Price = 9.99M,
Genre = "Comedy of manners"
},
new Book {
Title = "Northanger Abbey",
Year = 1817,
Author = austen,
Price = 12.95M,
Genre = "Gothic parody"
},
new Book {
Title = "David Copperfield",
Year = 1850,
Author = dickens,
Price = 15,
Genre = "Bildungsroman"
},
new Book {
Title = "Don Quixote",
Year = 1617,
Author = cervantes,
Price = 8.95M,
Genre = "Picaresque"
}
);
context.SaveChanges();
}
}
}
}
的用法project.json
与以下dependencies
part:
{
"dependencies": {
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.AspNet.Tooling.Razor": "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.Logging": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc1-final",
"Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",
"System.Net.Http": "4.0.1-beta-23516",
"Microsoft.Net.Http": "2.2.29",
"Newtonsoft.Json": "8.0.1"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
}
}
固定的appsettings.json
from
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
},
"Data": {
"DefaultConnection":{
"ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
}
}
}
}
to
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=localhost;Database=BookStore;User Id=sa; Password=******;Trusted_Connection=true;"
}
}
}
(Data
should 不低于Logging
)
以及以下内容的用法Startup.cs
using apiservice.Models;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Entity;
namespace apiservice
{
public class Startup
{
public static IConfigurationRoot Configuration {get; set;}
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<BookContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
// we need to execute the following two commands before
// dnu restore
// dnx ef migrations add Initial
// dnx ef database update
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<BookContext>()
.Database.Migrate();
}
}
catch { }
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
SampleData.Initialize(app.ApplicationServices);
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
我在里面的评论里写到Configure
执行命令的方法
dnx ef migrations add Initial
dnx ef database update
所有包都恢复后。dnx ef migrations add Initial
将创造额外的Migrations
项目中的文件夹,其中包含以下文件20160101205609_Initial.cs
and BookContextModelSnapshot.cs
。数据库中定义appsettings.json
将由创建dnx ef database update
它将填充来自的测试数据SampleData.cs
在程序分叉期间。
顺便说一句我搬家了package.json
包文件夹内(与project.json
)。您不使用这些包,但移动使 npm 包在 Visual Studio 中可见并进行管理。