Entity Framework Core Migrations in a desktop or mobile application - entity-framework-core

I don't want to sound like a jerk here, but it feels like the desktop and mobile applications have been forgotten when migrations were documented.
Given:
It is unacceptable to ask an end user to run any commands to create or migrate a database.
Its not an Asp.Net Core web application so I don't have a startup.cs scaffolded.
When the software is updated on an user's device it must update its own database without user intervention the next time it runs.
This is a code first project.
I'm pretty sure its just a matter of getting the IOC container that the entity framework core code will use and putting the right things into it, but I'll be damned if I can figure out how. Despite my best google-jitsu and bing-fu, I've only been able to find docs or examples that show running using startup.cs in web projects or using the Entity Framework core CLI tools.
public void SomeDesktopAppStartupMethod()
{
var context = new DesktopAppContext();
// ??? - register some migration locator with the IOC container? ¯\(°_o)/¯
context.Database.Migrate();
// Profit!
}

Right cause there really isn't much to the migrations of your database within in a mobile. Since more than likely you are using SQLite (guessing) mobile/uwp apps don't get some of the same treatment that Web gets this is a function of MS not pushing mobile at present if you hadn't noticed. Unfortunate for most of us early adopters but early bird doesn't always get the worm.
More than likely those doing Desktop are going to be using EF not EFC and not usually target SQLite but SqlExpress or SqlLocalDb
Most of my changes for my apps are surface level changes to the database but due to limitations of the migration system you would have to write your own SQL into the UP/Down of a migration to drop columns in SQLite and the reverse to undo your changes. The is a SQLite only limitation at present, for EFC provider.
As for location in the application where I process any migrations since I am using template10 (which is in a refactoring at present) is done where the UIElement CreateRootElement(IActivatedEventArgs e) is being created. Another location to consider would be OnStartUp for the app. It would vary based the development you are doing of course XPlat might make sense where all flavors could take advantage.
Some apps when syncing data I don't bother with migrations and wipe and recreate cause its just easier from that standpoint since all data is server based on Azure.
That being said there are major changes coming from 2.0 with respect to SQLite and EFC, if read correctly there will be a heck of a lot more control over how things are "migrated" or modified from Version X.X of your app.

We're using EFCore and SQLite for our Desktop app! This is what we did for the InitialCreate migration, I can't speak to subsequent migrations, but they should be similar (solution left as exercise to the reader):
1.For the project that contains the DBContext implementation, change the csproj ItemGroup section to include
2.Right click on the project and select Open folder in File Explorer
3.Enter cmd in the command window
4.In the command window enter: dotnet ef migrations add "xxx"
where "xxx" is the name of your migration e.g. InitialCreate, or AddNewTable
5.This resulted in some error messages for me, but in the end it created a
migrations folder with 3 new classes. Date_xxx.cs Date_xxx.Designer.cs and a YourDBContextClassModelSnapshot.cs. Add the folder and classes to your project/source.
6.Ensure your DBContext implementation has an empty ctor.
7.Replace your call to context.DataBase.EnsureCreated() with
context.DataBase.Migrate()
8.Rebuild, sacrifice a few chickens, cross your fingers and hopefully the Migrate() now creates a db with version/migration info. You can edit the .DB file which is mostly binary but will have some text like the following, which does not exist when created via .EnsureCreate()
"MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY,
"ProductVersion" TEXT NOT NULL
Notes: the first time I ran the dotnet command it failed because the EFCore designer/tools weren't loaded. I did some project rebuilds and it seemed to work after that. Also i had a lot of problems with the app during runtime getting "EFCore assembly failing to load" crashes due to different projects referencing different versions. Took many clean/rebuilds to get this working again once the .csprojs were tweaked to ref the same version. Also, out of curiosity i tried The "ef migrations remove" command, but it failed to execute (unable to load Sqlite3), fortunately we don't need to use that cmd yet.

Related

Entity Framework Core, what is the best way to run sql after DB is generating

i want run some code for triggers and other changes
is there an event to register?, something like OnDbCreate
If you want do some changes, you can use Up Method in your recently added Migration. Anything you change there, will be applied in the Database after you call Update-Database from Package Manager Console. You can even run custom Sql Query if you want.

SqlPackage - How do I stop it from turning off my query store?

I have a database project in Visual Studio 2017. Our database project is managed just like any other library of code where multiple developers can update the project as necessary. To ease the pain of deployments, I have built a custom deployment task in our TFS 2018 (vNext) Build process that is a powershell script that calls sqlPackage.exe. SqlPackage compares our compiled database project (*.dacpac file) to our target database (in Dev, QA, etc.). I have the custom step configured so that it will write the expected changes to disk so I have a record of what was changed, then sqlPackage runs a second pass to apply the changes to the intended target database.
My DBA enabled the Query Store in our SQL 2016 Database. During my sqlPackage deployment, one of the initial steps is to turn the query store off, this makes my DBA unhappy. He wants the ability to compare pre and post deployment changes but if the query store gets turned off, we lose the history.
I have tried several of the switches in the documentation (https://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx#Publish%20Parameters,%20Properties,%20and%20SQLCMD%20Variables) but I can't seem to find the magic parameter.
How do I stop SqlPackage from turning off the query store?
My current script:
sqlPackage.exe /Action:Script /SourceFile: myPath\MyDatabaseName.dacpac" /OutputPath:"myPath\TheseAreMyChangesThatWillBeApplied.sql" /TargetConnectionString:"Integrated Security=true;server=MyServer;database=MyDatabase;" /p:DropObjectsNotInSource=false /p:DropPermissionsNotInSource=false /p:DropRoleMembersNotInSource=false /p:BlockOnPossibleDataLoss=True /Variables:"CrossDatabaseRefs=CrossDatabaseRefs
Is there a better way? I am surprised that I had to write a custom TFS Build Task to do this. Which makes me think that I might be doing it the hard way. (But this has worked pretty well for me for the last several years). I love database projects, I love that they enforce references and ensure that we don't break other objects when a column is dropped (for instance).
Any insight would be greatly appreciated.
Either disable the scripting of database properties using /p:ScriptDatabaseOptions=false, or update the database properties in the project to reflect the desired Query Store settings.
To set the Query Store settings in the project, right-click the database project in Solution Explorer and open Properties. From there, in the Project Settings tab find the "Database Settings..." button. In the Database Settings dialog, click the Operational tab and scroll down to find the Query Store settings.
Apparently, all we needed to do was add a post deployment script to re-enable the Query Store. Hope this helps someone out there...
USE Master
ALTER DATABASE [MyDbName] SET QUERY_STORE = ON

Adding multiple oracle schemas in visual studio 2013 and EF

I have a serious problem that needs to be resolved.
In my server explorer in VS2013, I set up a connection to the database with valid user and password credentials.
So I have a schema called: Connect1, and it needs to have access to another schema called Connect2, both under the same data source name: Dev1. The user I'm connecting with is called Connect1, so by default I'm connected to the schema Connect1. I was able to verify that I have access permissions for this user to connect to Connect2.
Using ODP.NET Managed Driver, I was able to set this up. Putting all the connection details.
Configurations are shown in this page:
Using Oracle Developer Tools for Visual Studio
In order to allow the schemas to be viewed, I had to change the Filters tab, and add all the related schemas I'm using (adding Connect2). Then I click update, and I'm able to use the tables into my edmx file.
However, the problem occurs when I close Visual Studios. My filter gets lost, and my connection is back to using only Connect1.
This then destroys my table mapping in my edmx file with a series of
Error 11007: Entity type 'Table1' is not mapped
This is really frustrating as I'd have to keep on repeating the same procedure over and over again manually remapping all my tables.
What's confusing is when I update the data connection from my Server Explorer to include the second schema.. EF works and detects the tables; but my app config and the connection string has not been changed. So this leads me to believe that if I was to promote my project to production or test, that it will break since there's nothing in my project to show that Connect2 is needed.
Okay I realize the big pitfall that was never explained properly. I highly urge people to follow this if you ever want to use EF with Oracle and you're using another schema.
The pitfall is that since we introduced oracle's schema convention to the EDMX, there was no way of knowing how volatile the mappings would become. The reason why this was never a problem for microsoft is because there is no such thing as a schema in SQL server. To implement two databases in the same edmx file, there is some visible prevention that inhibits you from adding any tables whatsoever. After we introduce schemas, it becomes very fragile.
So what you have to do is follow the steps of adding the schema (as I explained in the question). Going to Filter tab, adding the schemas that way, and clicking the Update button.
Once that is applied to the server explorer, the edmx will have access to adding the tables from that schema. When you do, the EDMX has recollection of the schema embedded in its file. So if you ever do close visual studios, and reopen and run the project, it will still work.
Here are the few tips you want to AVOID:
Do not rename any of the entities and the columns (properties), even though it makes your code more readable. That's the first uh-oh. Since the mappings are very volatile, if you happen to mess up, remapping everything back again will be a chore.
The second uh-oh, is when you close Visual Studios, reopen it again, forget to reinclude your schema in Server Explorer, go to the edmx and do "Update model from database" to include anything else (but most importantly.. EVEN IF YOU DON'T DO ANYTHING) and you click OK, your model will still update regardless of non-changes, lose all the mappings that you previously made. That is what I mean by the maps being volatile.
In case that happens, and you didn't do the first uh-oh of renaming entities or the properties.. is to delete all your entities already there, and re-add them again.
The reason why you want to delete all the entities is because adding new ones with the same name will add a new entity with the same name + a number. So something like entity of Customer will still be there, adding the entity again will create a new entity called Customer1.
Sigh.

Entity Framework 4.1 Code First Changes in Relationships often do not take

I often get errors when running test cases after changing relationships. When checking the relationships on the data diagram I notice that they are often not reflecting any of the changes I did or only some of them, confusing the model. This only seems to be the case when creating the diagram within Server Explorer of Visual Studio. When creating the diagram in SQL Server the changes show up fine.
Unfortunately, this also affects testing the code, because I'm never sure if a fails because of a problem with a change in the relationships I did or Visual Studio not updating. Having to detach and attach the database each time is a bit of a pain.
The database is dropped and recreated correctly, but Visual Studio seems to run the code against a cached version. Had anybody else similar problems, any fix?
I've certainly seen issues where I've updated SPs/function imports and tried to update the associated complex type, the dialog window says 'update', 'delete' etc next to the columns changed, so it's certainly seeing the different, but when applying the update these often don't seem to take and I end up manually adjusting the complex type.
In the above example I'm not sure whether the correct approach is to delete and recreate the type, but the existence of the update functions seem to suggest that an update should be possible. As you say mine too feels like a caching issue and whilst I've not yet found a solution I will be interested to see if you can find a solution, if I make any ground on this I will post back here too!

How to properly delete and re-add Entity Data Model

newbie to Entity Framework here. Using VS 2010 and SQL Server 2008 express DB.
I was having trouble refreshing an Entity Data Model after adding new tables. So, I followed a suggestion I found here to just delete and regenerate the model.
I get to the "Choose Your Data Connection" section of the Entity Data Model Wizard and "Save entity connection settings in Web.config as:" is checked. However, my existing name has a 1 appended to it. For example MyDatabaseEntities is now MyDatabaseEntities1. Of course, I don't want the "1" appended. I killed the existing connection string in Web.config and removed all references to that name in my solution. Yet when I try to continue I'm presented with the following error:
"'MyDatabaseEntites' conflicts with an existing property name in the Application Settings. Please choose a different name"
I can't find a reference to that name anywhere in the solution. I can uncheck that option and it will continue, but it STILL won't add two of the tables in the database. Next, I completely shut everything down, rebooted, and tried again. This time I didn't get the error above, but two of my three new tables still do not get added to the edmx model.
Any ideas are appreciated. Also this seems like it's still pretty buggy even in VS2010/.NET 4. Help restore my faith...I feel like abandoning Entity Framework at this point. Based on my experience so far and some of the other issues posted here, I feel like I'm going to spend more time chasing Entity Framework strangeness than writing useful code.
UPDATE: I found a resolution. The designer doesn't display errors. You have have to look in the native XML (edmx file) to see the errors. See here: ADO.NET Entity Framework: Update Wizard will not add tables
The error is caused by leftover connection strings in settings in "App.Config" file (see solution explorer) and it's in this section:
"connectionStrings" (surrounded by angle brackets)
Just delete it from there.
Restart IIS if you develop web app.
Restart Visual Studio After Deleting The EntityDataModel Connection
String Line from web.config or app.config
Add New Entity Data Model and rename it like old name,
I don't know the specific answer to your problem, but I think I can recommend an approach that will clear up this conflict.
I generally break my EF work into a separate library. If I'm working on Cyberdyne.Terminator, I make an EF class library called Cyberdyne.Terminator.Data. That way, if you want to blow away the model and start over, everything is separate from your dependent files, and in particular, your web.config.
None of this is likely to fix the missing tables, probably. The one thing I've seen where this happens is if I add a table, and then delete it. It will not appear again in the "Add Tables" dialog (and you have to rename it to get it to show). But I thought that was fixed in EF 4.0. Dunno. If the problem persists you might try renaming the table, re-adding it, and then renaming it back if it shows up.
EF is a pain in the ass to be sure. The only thing I can tell you is that I've generally had to work through these headaches, learn the issue, and never be bothered by them again. If you can't stand EF, you might also look at NHibernate - I have a very strong impression of that as well.
Double check your connection strings, and keep scrolling to the right. Most often, when this happens to me, Visual Studio has appended a connection string behind another. I've done this a couple times, and wondered where my app config was (no, there is no app config, just web config in mvc), so the dialog is a little confusing. Sure enough, most likely that connection string is lurking directly behind another!
Just press
Ctrl+F
keys and in dropdown list select 'EntireSolution/CurrentProject', then press enter. where ever this name generated it will show you.
I get into web.config file. so delete that generated complete connection line from there. and re add it again. Then hope you may not get this message again.Thank you..
Actually this is frustrating, since you trying to see in app.config that if there's a another string and you see only 1 string, Actually What VS does is sometimes append second string in the same line. see this picture below
Does it look like there are 2 ConnectionStrings? (ACTUALLY YES)
Now, see this pic:
Also you can press Ctrl+ED to format the config file and see if there's another second string hiding somewhere. Hope this will help someone.
im so sorry for my english
but the solution is simply.
you have to delete the ado.net
then, go to app.conf and delete the connection string<>,
DEBUG your project and finally add the new database through ado.net
TO ADD ADO.NET
RIGHT CLICK ON YOUR PROJECT NAME, THEN, ADD NEW ITEM, DATA AND SEARCH ADO.NET...
I hope help you, have good day!
angelsantacruzm