我创建了一个新模型:
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" />