【发布时间】:2016-10-24 16:23:09
【问题描述】:
我已经开始学习 MVC 和 ASP.NET Core。我一直在关注在线视频教程以习惯 MVC,但在使用实体框架在 localDb 中创建数据库时遇到了问题。
应该在 localDb 中创建一个名为“AspNetBlog”的数据库(如果尚未创建)。这应该在覆盖 OnConfiguring() 方法时在 BlogDataContext.cs 模型中发生,如 BlogDataContext.cs 中所示。
我得到的错误是“FileNotFoundException: 无法加载文件或程序集'System.Data.SqlClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'。系统找不到文件指定。"
错误的完整输出可以在这里看到:https://gyazo.com/d8019f4b09732eb059bd291a7881b16b
请注意,我已通过进入 VS2015 的命令提示符并输入 sqllocaldb start 来确保 localDb 正在运行。
我不确定如何修复此错误并成功创建数据库。该教程于 2015 年初发布,当时仍在使用 ASP.NET 5,因此,由于我确信我正确地遵循了教程,因此我认为问题出在此处。
下面你可以看到我的代码。
BlogDataContext.cs
语句“Database.EnsureCreated();”时遇到错误到达了。 Debug 的输出状态为“Exception throw: 'System.IO.FileNotFoundException' in EntityFramework.Relational.dll”。
using Microsoft.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AspNetBlog.Models
{
public class BlogDataContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public BlogDataContext()
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var connectionString = @"Server=(LocalDb)\MSSQLLocalDb;Database=AspNetBlog";
optionsBuilder.UseSqlServer(connectionString);
}
}
}
Post.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace AspNetBlog.Models
{
public class Post
{
public long Id { get; set; }
public string Title { get; set; }
public DateTime PostedDate { get; set; }
public string Author { get; set; }
public string Body { get; set; }
}
}
PostsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AspNetBlog.Models;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace AspNetBlog.Controllers
{
public class PostsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(Post post)
{
if (!ModelState.IsValid)
return View(post);
post.PostedDate = DateTime.Now;
post.Author = User.Identity.Name;
// Create new instance of blog data context
BlogDataContext db = new BlogDataContext();
db.Posts.Add(post); // - What should be added
await db.SaveChangesAsync(); // - Save changes to DB (Execute)
return View();
}
}
}
project.json
在这里你可以看到我正在使用“EntityFramework.SqlServer”:“7.0.0-beta8”。在我正在关注的教程中,使用了 "7.0.0-beta4" 版本。我尝试使用与教程中使用的版本相同的版本,但它似乎与 .NET Core 不兼容。
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.1",
"EntityFramework.SqlServer": "7.0.0-beta8"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
感谢您的阅读。欢迎和赞赏任何建议。
【问题讨论】:
标签: entity-framework asp.net-core asp.net-core-mvc asp.net-core-1.0