0

I have written a program that is running and I cannot delete the database. Now I want to add a series of new properties to the database tables. I know that I should use migration, but the point is that I have written the connection string manually with ConfigurationManager class and its functions, not in app.config. But now I have encountered a problem to add a new migration and update the database.

My question here is how to do the new migration and database update as code functions in C# instead of using package manager console and its commands i.e add-migration & update-database.

After many searches, I found a series of solutions like this link https://stackoverflow.com/questions/20216147/entity-framework-change-connection-at-runtime?answertab=modifieddesc#tab-top and using IDBConnectionIntercaptor, but in The test environment creates this database face with the error Cannot attach the file '***' as database '+++' .

my connection function is : `

public static bool Connect(bool force = false)
        {
            bool result = false;
            
            var selectedDb = new EventsDataContext();
            
            selectedDb.ChangeDatabase
                (
                    initialCatalog: Maincsb.InitialCatalog,
                    userId: "",
                    password: "",
                    dataSource: Maincsb.DataSource 
                );
            EventsDataContext odb = null;
            try
            {                
                ConnectionInterceptor connectionInterceptor = new ConnectionInterceptor
                {
                    connectionString = ConnectionManager.Maincsb.ConnectionString
                };
                DbInterception.Add(connectionInterceptor);
                DbInterception.Add(new ModifyYeKeIntercpetor());
                
                odb = new EventsDataContext();
                odb.Database.Initialize(force);

                Seeder(Maincsb.ConnectionString);
                result = true;
            }
            catch (Exception e)
            {
                ErrorMessage.Show(e);
                result = false;
            }
            finally
            {
                odb.Dispose();
                odb = null;
            }
            return result;
        }

`

And if the database has already been created, it will face the error that the database has changed and new features will not be added to the database.

ROMO
  • 13
  • 4

0 Answers0