ASP.NET Core 2.0 Code First Migration - New Database - asp.net-core-2.0

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.

Related

Publishing ASP.NET MvC to Azure with SQLite - data fetching fails

Just created a simple ASP.NET MvC project, to list blood pressure measurements. I opted to use SQLite as a database as it is (supposedly) embedded into the project, therefore eliminating the need for an external database. Which is expensive, and the reason why I chose to go with SQLite. That way I would only need to host the web app, which is free, if I chose the free tier, F1.
Publishing through VS2022 is successful, and the app shows correctly, except it shows none of the measurements. Which renders the app ((no) pun intended) useless, at least as a cloud app. I have done some research, and changed the publishingsettings a couple of times, but this is how they look right now.
Configuration: Release
Target Framework: net6.0
Deployment Mode: Self-contained
Target Runtime: win-x86
File Publish Options: None of the options chosen
Databases: Default Connection - Use this connection string at runtime:
=> Data source=bloodpressuremeasurements.db
Entity Framework Migrations: BloodPressureContext (name of the DbContext)
- Apply this migration on publish: NOT chosen, since it gave me an exception and publish failed
Site Extension Options: Install ASP.NET Core Logging Integration Site Extension
- NOT chosen
I also tried changing the option for the db file to Copy To Output Directory: Copy always.
That didn't change a thing. What am I missing?
The website works now as intended, with all the data shown. It looks like the problem stems from scaffolding read and write methods, which made Visual Studio 2022 pull in EntityFrameworkCore.SqlServer. Which is not what I wanted, since I'm using SQLite.
That in turn created some service dependencies under Connected Services, one of them being SQL Server something. It also appeared under the Publish menu, and seems to have caused the compilator to view the connection string as an SQL Server database connection.
I created a new app, and copied the code from the first one. I was careful not to scaffold, as I only need a Get method, to show all measurements. I need none of the other methods in CRUD, neither Post, Delete, nor Update. I will add new measurements by running the app again locally, and read the measurements from a CSV file (did that in the beginning). Then I will publish the app anew, with the updated SQLite 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

broadleaf database tables does not persist after application shutdown

I have been able to run the DemoSite project after changing the in-memory HSQL database to MySql following the instructions given in the broadleaf commerce getting started instructions for changing databases .
Now when I run the Demosite application I find 183 tables get created and populated with data , but this is in existence only during the running of the application . Once the application is shut down only 4 tables remain in the database .
I am using broadleaf demosite version 4.0.0 and MySql Database . The 4 tables which remain after application shut down are named as follows : -
blc_bank_account_payment
blc_credit_card_payment
blc_gift_card_payment
blc_static_asset_strg
I have changed the relevant properties files "hbmddl2.auto" entry value to "update" so that the database tables don't get created and erased for every new run of the application but still have not been able to get rid of this issue .
I have searched a lot also posted to the relevant forums but no answer . SOF is my last resort . Please help .
When you first run DemoSite you might have blPU.hibernate.hbm2ddl.auto=create-drop in your site/src/main/resources/runtime-properties/development.properties file in order to generate database tables.
How you are running the Site/Admin? From ant tomcat target from IDE or deploying war in tomcat outside of IDE.
I hope you have changed blPU.hibernate.hbm2ddl.auto property value to update.
Now if you are running ant tomcat from IDE, after changing the above property first you need to maven install core/site/admin modules before running site/admin.
If you are running from IntellijIDEA when you stop the application it will drop the tables if you have blPU.hibernate.hbm2ddl.auto=create-drop, where as Eclipse will immediately terminates the process without running the drop phase.
The best thing to do is when you start the application first time , after database tables are created and sample data is populated then take backup of the schema. Then change blPU.hibernate.hbm2ddl.auto to update, run maven install. Now start the application.
Hope it helps.

Mvc5 database SQL, VS 2013

I need your help, I am doing Mvc project in VS 2013 with SQL database, I have created database first and then I complete my project, but now working on project I change data type of one column, and the run project, it gives this kind of error.
By "database first", I'm assuming you mean your using EDMX. The EDMX is a snapshot of your database at the moment is was generated. If you change something in your database, you have to regenerate the EDMX.

Entity framework migration and seeding specific update

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

Resources