0

データが入力された既存の SQL Server Express データベースがあります。

EF 5 を中心に構築された新しい VS ソリューションを作成しました。既存のデータベースから EDMX を生成し、app.config上のデータベースへのポイントで接続文字列を確認しました.\SQLEXPRESS

ドメイン オブジェクトのプロジェクトを作成しました。

DbContextジェネレーターを追加しました。ドメイン オブジェクトも生成されます。

すべてがビルドされ、コンソール アプリケーションが実行されます。

ただし、問題は、既存のデータベースを使用する代わりに、新しいデータベースが作成されることです。

指定した既存のデータベースを使用する代わりに、EF が新しい空のデータベースを作成するのはなぜですか? どうすれば修正できますか?

DbContextクラスは次のとおりです。

public partial class ContactsEntities : DbContext
{
        public ContactsEntities()
            : base("name=ContactsEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Address> Addresses { get; set; }
        public DbSet<AddressType> AddressTypes { get; set; }
        public DbSet<Person> People { get; set; }
        public DbSet<Phone> Phones { get; set; }
        public DbSet<PhoneType> PhoneTypes { get; set; }
}

そして全体app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="ContactsEntities" connectionString="metadata=res://*/PeopleModel.csdl|res://*/PeopleModel.ssdl|res://*/PeopleModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local)\sqlexpress;initial catalog=Contacts;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

T4Scaffolding を使用してスキャフォールディングされた PeopleContext は次のとおりです。

   public class PeopleContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, add the following
        // code to the Application_Start method in your Global.asax file.
        // Note: this will destroy and re-create your database with every model change.
        // 
        // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<DataLayer.Models.PeopleContext>());

        public DbSet<Domain.Person> People { get; set; }
    }
4

0 に答える 0