このチュートリアルに従って、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文字列を返しませんでした
ハイムとガンサーの提案に従って、私は余分な接続設定を削除しました。
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
-を設定したのに、デフォルトのものは機能しないと思いますか?
LocalDbConnectionFactoryコンストラクターv11.0を使用すると、sqlserver 2012に接続でき、デフォルトでは、app.config内の接続文字列の名前をコンテキストのフルネームとして検索しようとします。configで接続文字列を指定しておらず、Testという名前のデータベースを(以下で指定するデータディレクトリに)作成する場合は、次のようにコンストラクターを設定する必要があります。
[DbConfigurationType(typeof(ImgSigDbConfig))]
class MyDbContext : DbContext
{
public MyDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
public DbSet<Person> Persons { get; set; }
}
これで、コンテキストを呼び出すことができます。
AppDomain.CurrentDomain.SetData("DataDirectory", @"c:\App\DataDirectory");
MyDbContext myDbContext = new MyDbContext("test"); //this is the name
myDbContext.Persons.Add(new Person() { Id = 1, Name = "Name1" });
myDbContext.SaveChanges();
構成は同じままです。
class ImgSigDbConfig : DbConfiguration
{
public ImgSigDbConfig()
{
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
}
}
データディレクトリはWebアプリではないため、手動で設定していることに注意してください。新しいデータベースを作成して接続できるように、このディレクトリにアクセス許可を与える必要があります。私のapp.configは次のようなものです:
<configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings>
</connectionStrings>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加