How can I update over a linked server with code first migration? - entity-framework-6

I am using entity framework 6 with code first. During deployment I need to make an update across and existing linked server. When I try this I get:
"MSDTC on server 'myLocal' is unavailable."
This problem happens when running agianst my local instance. MSDTC is not enabled on that instance but I have no trouble running this update manually in SQL Server. I don't want every developer to have to enable MSDTC for this one command.
I have tried adding this as a migration using a DbMigration.Sql and I get the error. I have also tried it in the seed method of DbMigrationsConfiguration using context.Database.ExecuteSqlCommand and I get the error.

ADO.NET Transaction scopes that go across servers - including linked servers - will automatically start the Distributed Transaction Coordinator (DTC). I think your best bet is to update the other database with a direct call in the migration, rather than through the linked server.

Related

Database recreated everytime Application runs

Good day guys,
I noticed my application deletes database structure and re add new one everytime i Run my application.
The DBset uses DropCreateDatabaseIfModelChanges. I have read about the other options like CreateDatabaseIfNotExist. is there any other option to force it from recreating database?
And also with throwing any exception of error?
I have been able to resolve this by using Code First Migrations

Web deploy task failed: data loss might occur when deploying to azure

I have MVC project which uses EF code first and I'm trying to publish to azure from Visual Studio but I'm receiving error: "Web deploy task failed: data loss might occur". I did some refactoring including renaming columns and I'm aware why the error occurs but I would like to force the migration because I'm sure that I handled the data loss:-) Nevertheless I have no idea how to skip data loss check. I've found that on SQL project you have option in properties that you can uncheck the 'block potential data loose' but I cannot find anything like that on my MVC project. I've tried to include my own script for schema update without the checks for data loss but EF complains that there are pending migrations, so I've tried to copy missing entries to _MigrationHistory table from my development db but it turned out that it's not that simple ;-) Because my app is still in development phase I have reinitialized db but It will be worth to know how to handle that kind of situation on "real" production environment:-)
Edit:
After some testing I've discovered that when publishing to azure there is now option "update database" which by default generates db update script based on diff on the local and azure db. It differs from the old "Run Code First migration" because the the old one was changing Db initializer to MigrateDatabaseToLatestVersion and on application start the db was migrated & seed was run when there were not applied migrations. The "update database" process in other hand is handled only by generated script and the migrations files and MigrationsHistory table is not used on production, neither the seed method. I was confused on the beginning but now it seems logical that update script gives more control over the database change, you always can modify the script, and furthermore the publish process of moving new code to azure performs only after successful db update.
These is an option called AutomaticMigrationDataLossAllowed, set this to true. And run Update-Database -Force. That should do it.

Database is not getting created at first time

How to re-create the database using EF6?
I have tried both of following post but, i don't know why its not working and getting same error.
How do I generate an EF6 database with migrations enabled, without using update-database?
Migrations is enabled for context ''but the database does not exist or contains no mapped tables
I have already published my sample on the web server. I am using Sql Server 2012 Express DBMS.
When i have created my application and published it on web server it was working fine. It has created database automatically. After that i have made some changes in ApplicationDBContext and added some properties in IdentityModes(ApplicationUser). Then I have migrated database on my Local IIS Express and it was working fine. Then I have tried to publish my sample again on web server but, this time it shows an error.
Migrations is enabled for context 'ApplicationDbContext' but the database does
not exist or contains no mapped tables. Use Migrations to create the database
and its tables, for example by running the 'Update-Database' command from the
Package Manager Console.
I have deleted the database on web server and tried again. but, still i am facing same error. Now, there is an empty database without table.
Application is creating database on User Registration.
I have also read this post and tried to call dbContext.Database.Initialize(true); and ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabase() method but, still its not working.
First, automatic migrations are nice for development but it's a hugely bad idea to rely on them in production. Second, at some point you turned off automatic migrations, which is why your production database is no longer having tables created in it.
Go ahead and leave automatic migrations off; even in development it's better to know exactly what changes you're making to your database by generating a migration, seeing the code that will be executed, and only then running Update-Database to apply those changes.
You have a number of choices in terms of getting schema updates into production, but they all boil down to roughly the same procedure.
Generate a new migration against your local database.
If you want to update your production database via SQL, run Update-Database -Script. This will generate a SQL file with the code to run on the database to migrate the schema. Hand this off to your DBA if you have one or review the SQL code yourself and then apply it manually to your database via Management Studio.
Run Update-Database. This time without -Script to make the changes to your local database schema.
If you didn't generate the SQL file in step 2, view your local database in SQL Server Object Explorer in Visual Studio. Right-click on the local database there and choose, "Schema Compare...". Follow the wizard to compare with your production database. In the end, you can either generate a SQL file with the necessary changes that need to be made (which again, you'd hand off to your DBA if you have one), or you can migrate to production directly from Visual Studio. However, this is not really recommended. It's always best to generate the SQL, which you or your DBA can then inspect before applying the changes live.
Chris Pratt is correct BTW.
I had downloaded a project in which I needed to just run "Update-Database
Here are the quick screenshots
Then at the bottom of Visual Studio
PM> Update-Database
DONE

Changing connection string from dropdown

I created a basic MVC app to do CRUD operations on database tables. I'm using Entity Framework Database First, so I was able to just scaffold out controllers and views very quickly.
We have a dev, qa, and prod database. The schema should be identical with only the data in each being different.
I want to create a dropdown that will let me change the connection string from the app. I'm not sure the best way to do this. It currently works just fine using the dev connection string, but if I manually change the database to qa from web.config, whenever I try to create or update an existing record from the app, I get this error:
"The UPDATE permission was denied on the object"
I don't get this error using the dev database.
The problem is with your database permissions.
Use the following link and go through the security/permissions and you will be able to solved the problem.
http://blog.sqlauthority.com/2012/04/23/sql-server-introduction-to-sql-server-security-a-primer/
Please verify that your user has the same permissions on qa/prod as it does in dev.

EF Migrations - how to manage during dev and deployment?

We're considering using EF 4.3.1 code-based migrations, but aren't clear about how to integrate Migrations with our present dev/deployment methodology...
The app in-question is a desktop WPF app, with each desktop having its own SQL Server instance (each with 4 separate databases). It is deployed into a "field" environment with zero local IT support. Any database migration must be done using SQL scripts executed by the installer (probably InstallShield). There will not be anyone available who can run a command at a PMC prompt to upgrade the db when it is deployed/upgraded at a field location. Thus the ultimate "output" from EF Migrations must be a set of SQL scripts, which the installer will selectively apply.
Also, we have multiple developers making concurrent database changes.. there is NO DBA. Each developer simply checks-in their code (model) changes to TFS, and the next time they do get-latest, the changes to the model automatically cause a new database to be created on their dev system. So how can we now have each developer perform their own local migrations (rather than deleting/recreating their local databases), and then manage/consolidate/combine those migrations? And what about collisions?
During dev and unit-testing, each developer may delete their (entire) local database multiple times during a single checkout/checkin iteration. This works great with Code First, since the database gets automatically rebuilt when the app is restarted. But this means that the _MigrationHistory table in the database also gets deleted. How do we handle this? Don't we need the migration history of each dev system? If not, then where/how do we detect the aggregate changes which need to be applied to the delivered system?
I can see the value of using Migrations to deal with the mechanics of migrating a database, but what's not clear is how to take advantage of it without introducing a centralized database "change-control" bottleneck into the dev cycle, and thus losing one of the key benefits of Code First.
Any insight/advice would be greatly appreciated!
DadCat
I know this is an old question but I thought I'd post some of my experiences with EF Migrations (v6.1).
Each dev will be fine. Migrations are put into classes with a timestamp in the name, so no collisions will happen. The DB on the dev's machine will be updated after doing a get latest and running the app (or the update-database command).
Deleting the local db and recreating is fine. Just make sure the dev runs update-database before adding additional migrations or things will get out of sync. I'm a bit confused as to why they'd need to delete the local DB, but that's out of scope. You may find that your process needs to change to accommodate EF Migrations.
I can't help you with the installer question, as a similar question brought me here. The update-database command does have a -script option that will generate the proper change script, but I'm unclear how to automate that on a build server.

Resources