Entity framework migration and seeding specific update - entity-framework-5

does anyone know how you can seed a specific update using EF 5 migrations? I have existing database, alredy has lookups populated, and am developing some Audit functionality. I have created an AuditType entity and Audit. When I call update-database, I don't want all my seed data from when I first created the database to be re-added. Do I simply have to manuall delete the existing seed data out or can I do something like name a Configuration.cs with the datetime similar to what gets created when I call add-migration?
Thanks

You can run a specific migration by specifying the name of the migration. For example, If you have a migration called MyTuesdayMigration.cs, In the package manager console, you would run this command:
update-database -TargetMigration MyTuesdayMigration

You may need to delete data so you should use -fore
update-database -TargetMigration MigrationName -force

Related

Cosmos DB - Microsoft.Azure.Documents.AddressResolver.EnsureRoutingMapPresent

Ive been getting some odd issues with Cosmos DB as part of a data migration. The migration consisted of deleting and recreating our production collection and then using the Azure Cosmos DB migration tool to copy documents from our Development collection.
I wanted a full purge of the data already in the production collection rather than copying the new documents on top, so to achieve this I did the following process…
Deleted the production collection, named “Production_Products”
Recreated the Production collection with the same name and partition key
Using the Azure Cosmos DB Data Migration Tool, I copied the documents from our development collection into the newly created and empty production collection “Production_Products”
Once the migration was complete we tested the website and we kept getting the following error…
Microsoft.Azure.Documents.NotFoundException: at
Microsoft.Azure.Documents.AddressResolver.EnsureRoutingMapPresent
This was very confusing as we could query the data from Azure no problem. After multiple application restarts and checking the config we created a new collection “Production_Products_Test” and repeated the migration steps.
This worked fine. Later in the day we reverted our changes by recreating a new collection with the original name “Production_Products” and that failed. We had to revert back to using the “_Test” collection.
Can anyone offer any insight into why this is happening?
Based on the comments.
The DocumentClient maintains address caches, if you delete and recreate the collection externally (not through the DocumentClient or at least, not through that particular DocumentClient instance since you describe there are many services), the issue that might arise is that the address cache that that instance has is invalid. Newer versions of the SDK contain fixes that would react and refresh the cache (see the Change log here https://learn.microsoft.com/azure/cosmos-db/sql-api-sdk-dotnet).
The SDK 2.1.3 is rather old (more than 2 years) and the recommendation would be to update it (2.10.3 is the latest at this point).
The reason for the invalidation of those caches is that when you delete and recreate, the new collection has a different ResourceId.
Having said that, there is a scenario that won't be easily fixed, and that is if when you delete and recreate a collection, your code is using ResourceIds (for example, using the SelfLinks) instead of the names/ids to do operations. In those cases, if you are caching or holding a reference to the ResourceId of the previous collection, those requests will fail. Instead, you would need to use the names/ids through UriFactory.
Normally in these cases knowing the full stack trace of the exception (not just the name of the type) helps understand what is going on exactly.

ASP.NET Core 2.0 Code First Migration - New Database

I currently have an ASP.NET Core 2.0 project and I've just implemented code first migrations to the localdb and have around 3 migrations including the initial create. I'm wondering what the correct process is to change to a new database? Is it simply a case of updating the connection string and running the below in the project directory?
dotnet ef database update
My current knowledge is based on the below Microsoft tutorial and I've been using the CLI commands.
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
In answer to my own question, it does appear that running the following command in the existing project directory will build a new database from scratch including all the existing migrations. As long as you update the connection string to the new database and have already implemented migrations on the existing database.
dotnet ef database update
correct process is to change to a new database
Everything depends what you mean by writing this sentence ( I am not sure what "new database" means in it) altough flow looks like this:
1. First you make initial create
2. Then you change something in your code (create some additional fields etc)
3. Then you write "dotnet ef database update" command in CLI to update your current database.

How do you tell entity framework to create / deploy a database in Azure?

I'm using Entity Framework with a code first model; I've got my InitialCreate migration setup and working locally, I can run code against my database context, and everything works.
But when I deploy my project to Azure, I just get a connection string error ("Format of the initialization string does not conform to specification starting at index 0.").
I can't seem to find where in the Publish dialog are the options to create the Azure database. -- Do I have to create the database separately and hook them up manually? -- If so, what exact process should I follow. Does the database need to have contents?
I thought Microsoft was making a big deal that this could all be done in a single deploy step, but that doesn't seem to be the case from my current experience.
When you publish your project in the publish dialog, there is an option for the code first migration in the Settings tab, it will automatically show your data context and it will give you the option to set the remote connection string, and this will add a section in web.config to specify the data context and the Migration class to run during the migration process.
It will also allow you to set if you want to run the code first Migration or not.
You can also take a backup from the dev and clear the data then upload it to Azure SQL DB, this way the code first data context will check at first connection and it will find the code an database the same

How to enable the Migrations in My MVC5 Project?

Following Error i get it,
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 Consol
You need to perform this commands in Package Manager Consol
Enable-Migrations
Add-Migration "Initial"
Update-Database
There you will have a database with migrations based on your models classes

Bootstraping an application, is triggers a good idea?

I'm a building an internal webb application for components of building parts. I have table with projects which is tied to some other tables. When a user creates a new project, I want to "bootstrap" the project with a default categorization schema, which the user then can modify for his/her project. So I need to do some copy from a default schema and tie it to the users project.
I'm running NodeJS on backend, AngularJS on frontend and postgres as db. Where is the best way to put this logic? Either I use triggers on the db. The trigger is activated when a new post is made to the project table. Or, I'll do it with complicated queries in Node. Or is there some other way? Is there a best practice? It's probably "easier" to do a trigger. But I worry about the maintenance and testing of the app.
Since the issue that you have is related to the state of the database, you should solve it inside the database. There are basically two ways of solving this:
Revoke the insert privilege on the project table. Create a function new_project() that has parameters for all the required initial state of the project. Inside that function you create schema, do some copying, setup privileges and populate the tables with the parameter values.
Revoke the insert privilege on the project table. Create a view that has all required columns from all relevant tables to make a valid initial project and create an INSTEAD OF INSERT trigger on the views. In the trigger function you perform all the required steps as above.
Debugging code on PostgreSQL is not very advanced but whether or not you place you code in PostgreSQL or on the application side, you will have the same issues. The advantage of PostgreSQL is that the bug - if any - is never far away from where you code operates.

Resources