按照本教程,我创建了我的DbConfiguration
课程:
class ImgSigDbConfig : DbConfiguration
{
public ImgSigDbConfig()
{
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
}
}
并告诉我DbContext
使用它:
[DbConfigurationType(typeof(ImgSigDbConfig))]
class ImgSimContext : DbContext
{
这似乎连接得很好,但我不知道它在哪里存储我的数据,我不喜欢那样。我希望它将所有内容保存到 App_Data 文件夹中的文件中。如何从ImgSigDbConfig
构造函数中指定它?(假设我对 SQL 服务器连接一无所知)
做了一些更多的挖掘并想出了这个:
public ImgSigDbConfig()
{
var sqlConnBuilder = new SqlConnectionStringBuilder();
sqlConnBuilder.DataSource = ".";
sqlConnBuilder.InitialCatalog = "ImgSim.ImgSimContext";
sqlConnBuilder.IntegratedSecurity = true;
sqlConnBuilder.AttachDBFilename = @"App_Data\database.mdf";
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0", sqlConnBuilder.ToString()));
}
但现在它抛出:
提供者未返回 ProviderManifestToken 字符串
根据 haim 和 gunther 的建议,我删除了多余的连接设置:
class ImgSigDbConfig : DbConfiguration
{
public ImgSigDbConfig()
{
var sqlConnBuilder = new SqlConnectionStringBuilder();
sqlConnBuilder.IntegratedSecurity = true;
sqlConnBuilder.MultipleActiveResultSets = true;
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0", sqlConnBuilder.ToString()));
SetManifestTokenResolver(new DefaultManifestTokenResolver());
}
}
并将它们移到 Program.cs 中:
var conn = new SqlConnectionStringBuilder
{
AttachDBFilename = @"App_Data\database.mdf",
//InitialCatalog = "ImgSim.ImgSimContext",
};
using (var db = new ImgSimContext(conn.ToString()))
但即便如此,我还是得到了System.Data.Entity.Core.ProviderIncompatibleException
InnerException:
提供程序未返回 ProviderManifestToken 字符串。
即使我设置了SetManifestTokenResolver
- 我猜默认的不起作用?