EF Code First Existing Databases

First, using EF 4.1 Code, I wanted to stop creating an EF database from models. As far as I understand, if you pass the name Connectionstring, it will use the existing database from this connection string. However, if the database does not exist, then it will create the database, and all the tables form a model.

  <connectionStrings>
<add name="DiaryContext" connectionString="Data Source=.;Initial Catalog=Diary;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

        public MyDiaryContext() : 
        base("name=DiaryContext")
    {
        this.Configuration.LazyLoadingEnabled = true;
        this.Configuration.ProxyCreationEnabled = false;
    }

In the above example, it behaves as follows:

(1) if the database does not exist, it will create a database with all tables, etc.

(2) if the database exists and it does not have tables, then it will not create new tables

(3) if I add a new model of EF code, then it will not create a new table in the database.

2 3, (1), , , db .

DBA, .

- , ?

,

+3
1

EF-, I, , DataContext

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<WebContext>());

, DropCreateDatabaseIfModelChanges DropCreateDatabaseAlways, . , , , - DB , , , db. scot gu

+5

All Articles