关于EF6.0的一些BUG

这些天在研究EF Code First模式来开发新的项目,但在使用EF的时候发现了一些坑,在此列出来,希望大家不要跳到这个坑里面。

1.正常我们新建好一个项目后,会使用Nuget添加EF:

  Install-package EntityFramework -Version 6.0.0 

  Install-Package EntityFramerwork.zh-hans

2.新建BaseDbContext类来继承DbContext:  

Public Class BaseDbContext:DbContext

{

  public BaseDbContext():base("name=SqlserverCon") //为app.config或web.config里面的数据库连接字符串

  {    

  }

  public virtual DbSet<User> User { get; set; }

}

3.以上是创建好我们自己的Dbcontext,接下来我们就要启用数据迁移,在控制台输入:Enable-Migrations 报以下错误:

No connection string named 'SqlserverCon' could be found in the application config file.

提示在app.config找不到SqlserverCon连接字符串,这刚开始以为是app.config文件配置错误,打开配置文件又能找到SqlserverCon,明明有但为啥就提示找不到呢?结果折腾了半天都没有解决问题。不晓得是不是EF 6.0BUG,最后将BaseDbContext构造函数中的("name=SqlserverCon")去掉name字段直接写成("SqlserverCon").在此运行Enable-Migrations,虽然不提示上面错误,但问题又来了:

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

上面错误意思大概是连接数据库错误,找不到提供的数据库。结果又去检查字符串连接,发现没有什么问题,这个字符串我一直在用,在EF上面就不能用了?结果有折腾了半天,最后在Nuget 里面更新EF :

  Install-package EntityFramework

再次运行:Enable-Migrations

PM> Enable-Migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project Lixise.RemoteControl.DalMsSql.

提示当前项目已经启用Code First数据迁移,到此上面2个问题通过更新EF到最新版本和删除name字段得到了解决,但在后面在添加实例到数据库时有出现错误,将在下篇文章来写出来,解决问题步骤。

posted @ 2015-11-14 10:37  hack301  阅读(723)  评论(0编辑  收藏  举报