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"  />
4

1 回答 1

1

我认为问题在于 EF 尝试从您的项目中再次创建 MDF。尝试先删除它。

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

于 2013-05-08T09:03:38.183 回答