【问题标题】:Error FileNotFound System.Data.SqlClient when creating new database using ASP.NET Core and Entity Framework使用 ASP.NET Core 和 Entity Framework 创建新数据库时出现错误 FileNotFound System.Data.SqlClient
【发布时间】: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


    【解决方案1】:

    现在有一个 Entity Framework Core 的候选版本。尝试更新您的 project.json 以指向它。

    【讨论】:

    • 尝试将 "EntityFramework.SqlServer": "7.0.0-beta8" 替换为 "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1" 并且效果也很好。谢谢,我将继续使用它。
    【解决方案2】:

    最后自己修好了。在“project.json”文件中,我添加了以下依赖项:

    "System.Data.SqlClient": "4.1.0"

    确保它是这样写的,而不是小写字母。这实际上是我尝试过的第一件事,但我一开始是用小写字母写的,但控制台出错了。我正在做的教程是“使用 ASP.NET 5 启动并运行”。 (所以像我这样正在做本教程并遇到同样问题的其他人可以找到这个,以防万一)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-08
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 2017-06-01
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多