实体框架在加载时引发异常

时间:2019-10-29 08:57:04

标签: c# entity-framework entity-framework-6

我正在尝试在Entity Framework中编写我的第一个应用程序。

program.cs(我的main()所在的位置),我有ProdContext ctx = new ProdContext();行。 prodContext.cs是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Text;

namespace Entity_Framework
{
    class ProdContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
}

在尝试运行main时,在上一行出现异常:


FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

我用C#和EF重新安装了Visual Studio(我用install-package entityframework -version 6.1.3.0安装了它)。为什么我会收到该例外?

1 个答案:

答案 0 :(得分:2)

我建议您在尝试安装.NET库的同时使用.NET Core应用程序。 EF 6是“ .NET”特定的库。对于您的项目,如果使用.NET Core应用程序类型,则在安装.NET EF版本时会发生此类崩溃(或类似的崩溃):

https://developer.android.com/jetpack/androidx/releases/camera

对于.NET Core应用程序,您应该安装Core版本。 enter image description here

确保引用中没有此(或类似错误)错误:

https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/