2013-05-08 50 views
0

我创建了一个新的模式:实体框架 - “不正确的连接字符串” 错误

public class AuthorDatacontext : DbContext 
    { 
     public DbSet<Author> Authors { get; set; } 

     static AuthorDatacontext() 
     { 
      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AuthorDatacontext>()); 
     } 
    } 

连接字符串:

<add name="MyLibrary.Models.AuthorDatacontext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MyLibrary.Models.AuthorDatacontext.mdf" providerName = "System.Data.SqlClient" /> 

,我用它在我的控制器:

var db = new AuthorDatacontext(); 
       db.Authors.Add(author); 
       db.SaveChanges(); 

我之前做过,在另一个项目中,它运行良好。 但现在我收到一个错误,试图保存新作者:

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. 

我到底做错了什么? 如果我删除连接,并只保留默认连接,一切工作正常。

以前的项目有相同的连接字符串:

<add name="MvcAuction.Models.AuctionsDataContext" connectionString="Data Source= (LocalDb)\v10.0; Integrated Security=SSPI; AttachDBFilename = |DataDirectory|\MvcAuction.Models.AuctionsDataContext.mdf" providerName = "System.Data.SqlClient" /> 

回答

1

我认为问题是,EF试图从你的项目再次创建MDF。尝试先删除它。

\MvcAuction.Models.AuctionsDataContext.mdf EF试图查看它是否存在于该位置。如果您使用过SQL服务器,它会将更改从CODE First迁移到数据库。

+0

但它的另一个项目。它为什么寻找它? – 2013-05-08 09:22:09

+0

您需要一个数据库来初始化您的EF上下文 – 2013-05-08 09:31:57

+0

我如何创建它?为什么我在第一个项目中没有这个问题? – 2013-05-08 09:34:00

相关问题