我正在构建一个webbapplication来学习ASP.NET 5.我正在使用:
Visual Studio 2015社区
ASP.NET 5 RC1
MVC 6
实体框架7
问题是在创建我的dbcontext以将实体映射到数据库之后,应用程序由于一个争论而无法启动。请参阅下文并注意参数无效的其他信息(Nyckelordetstödsinte):multipleactiverecordsets。我该如何解决这个问题?
当我将以下内容发表评论时,该项目能够运行:
AppController.cs
public IActionResult Index()
{
//var textposts = _context.Textposts.OrderBy(t => t.UserName).ToList(); //here
//return View(textposts); //here
return View();
}
ProjectContext.cs
public class ProjectContext : DbContext
{
public ProjectContext()
{
//Create the database and execute migrations
//Database.EnsureCreated(); //here
//Database.Migrate();
}
以下是有关问题和代码段的信息。
Error. EF7 DB argumentexception multipleactiverecordsets
我准备好了数据库并进行了初始迁移(使用“dnx ef迁移添加InitialDatabase”时创建了迁移文件夹并将cs文件添加到该文件夹中)
Startup.cs
public class Startup
{
public static IConfigurationRoot Configuration;
public Startup(IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ProjectContext>();
project.json
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"EntityFramework.MicrosoftSqlServer": "7.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",
"EntityFramework.Commands": "7.0.0-rc1-final"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"
},
"frameworks": {
"dnx451": { },
"dnxcore50": { }
},
"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
ProjectContext.cs
public class ProjectContext : DbContext
{
public ProjectContext()
{
//Create the database and execute migrations
//Database.EnsureCreated();
//Database.Migrate();
}
public DbSet<Textpost> Textposts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connString = Startup.Configuration["Data:ProjectContextConnection"];
//Optionsbuilder exposes the entities, using SQL Server.
//Other types of db servers could be shown here if configured, such as MongoDB
optionsBuilder.UseSqlServer(connString);
base.OnConfiguring(optionsBuilder);
}
}
}
AppController.cs
public class AppController : Controller
{
private IMailService _mailService;
private ProjectContext _context;
public AppController(IMailService service, ProjectContext context)
{
_mailService = service;
_context = context;
}
//The request is called Index. No id parameter in here yet -> app/index/id
public IActionResult Index()
{
var textposts = _context.Textposts.OrderBy(t => t.UserName).ToList();
return View(textposts);
//return View();
}
此外,当我尝试使用dnx ef命令工具添加新的迁移时,显示短语“参数无效(Nyckelordetstödsinte):multipleactiverecordsets”。
dnx ef migration add SecondMigration
编辑: 我在config.json中的连接字符串
"Data": {
"ProjectContextConnection": "Server=(localdb)\\ProjectsV12;Database=ProjectDb;Trusted_Connection=True;MultipleActiveRecordSets=True;"
}
答案 0 :(得分:2)
ErikEJ通过询问连接字符串来解决问题。
在关注如何设置数据库和连接字符串的教程之后,我现在将字符串的最后一部分从MultipleActiveRecordSets更改为MultipleActiveResultSets。也许第一个符号来自以前的版本,现在不支持。它现在正在工作,感谢小费和热烈的欢迎!