配置Entity Framework连接Sql Server出现的一个异常

    异常:The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

 

    方法1

      网上找到这样一个方法,在代码中添加:

      public void FixEfProviderServicesProblem()
      {
        //The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
        //for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.
        //Make sure the provider assembly is available to the running application.
        //See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

        var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
      }

      本段代码并未使用;至于为什么会消除异常,还待深入了解。但这不是一个好的解决方法,它可能会引起另外一些问题。

 

     方法2

      在配置有Entity Framework的子项目中,有几个DLL如下:

      

      出现异常的原因主要是缺少了EntityFramework.DynamicFilters.dll、EntityFramework.SqlServer.dll、EntityFramework.SqlServer.xml。将这三个文件拷贝到主项目中,即可。

 

转载于:https://www.cnblogs.com/xuanxiaochen/p/5683660.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# Entity Framework 绑定 SqlServer Express 的 C# 示例 下面是一个使用 Entity Framework 绑定 SqlServer Express 的 C# 示例: 1. 首先,确保你已经安装了 SqlServer Express 和 Microsoft.NET Framework。如果没有,请先下载并安装。 2. 打开 Visual Studio,创建一个新的 C# 项目。 3. 在项目中添加 Entity Framework。在解决方案资源管理器中右键单击“引用”,选择“管理NuGet程序包”选项。 4. 在“NuGet程序包管理器”中搜索“EntityFramework”,然后安装程序包。 5. 在代码文件中,添加以下命名空间: ``` using System.Data.Entity; ``` 6. 创建一个派生自 DbContext 的类。这个类将代表你的数据库上下文。 ``` public class MyDbContext : DbContext { public DbSet<MyEntity> MyEntities { get; set; } } ``` 在上面的代码中,MyDbContext 类继承自 DbContext,并且具有一个名为“MyEntities”的 DbSet 属性。这个属性代表数据库中的一个实体集合。如果你需要处理多个实体,你可以在 MyDbContext 类中添加多个 DbSet 属性。 7. 创建一个派生自 DbContextInitializer 的类。这个类将在 MyDbContext 类实例被创建或数据库首次使用时被调用。 ``` public class MyDbContextInitializer : CreateDatabaseIfNotExists<MyDbContext> { protected override void Seed(MyDbContext context) { base.Seed(context); } } ``` 在上面的代码中,MyDbContextInitializer 类继承自 CreateDatabaseIfNotExists,并重写了 Seed 方法。这个方法将在数据库首次使用时被调用,可以在这个方法中添加一些初始化数据。 8. 在你的代码中,创建 MyDbContext 的实例并使用它来访问数据库。 ``` using (var db = new MyDbContext()) { // 具体操作数据库的代码 } ``` 在上面的代码中,你创建了 MyDbContext 的一个实例,并在 using 块中使用。在 using 块结束时,MyDbContext 实例将被销毁。 9. 通过连接字符串指定连接 SqlServer Express 数据库。可以在 App.config 或 Web.config 文件中添加以下代码: ``` <connectionStrings> <add name="MyDbContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> ``` 在上面的代码中,Data Source 指定了 SqlServer Express 实例的名称(在这里是“.\SQLEXPRESS”),Initial Catalog 指定了要连接数据库名称(在这里是“MyDatabase”),Integrated Security=True 指定了使用 Windows 身份验证连接 SqlServer Express。 10. 在 MyDbContext 类中使用上面指定的连接字符串: ``` public class MyDbContext : DbContext { public MyDbContext() : base("name=MyDbContext") { } public DbSet<MyEntity> MyEntities { get; set; } } ``` 在上面的代码中,MyDbContext 类的构造函数使用了连接字符串名称“MyDbContext”。这个名称与前面在 App.config 或 Web.config 文件中定义的名称相同。 现在你已经知道了如何使用 Entity Framework 绑定 SqlServer Express 的 C# 示例。你可以在你的代码中使用这些示例来访问 SqlServer Express 数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值