none
Entity Framework Code First update dabase on client computer RRS feed

  • Question

  • Hope I'm posting this in the right forum (first post). I did some searching but got really confused. Mind you, I'm a beginner with VS / EF / MSSQL

    I'm developing a Windows Desktop app using MSSQL and Entity Framework.

    Working on my dev computer using the code first approach with migrations and updating the database is no problem (using the console).

    But in the future the app will be running on client computers. How do I update the database on the client computer? Can I run the migrations from within the application exe file? 

    Also is this a good approach or not?


    Wednesday, March 21, 2018 1:56 PM

Answers

  • Hi rbojanssen,

    >>But in the future the app will be running on client computers. How do I update the database on the client computer? Can I run the migrations from within the application exe file? 

    If dev machine and client machine use different MSSQL. A simple way is that you backup database from your dev computer and restore on your client computer without any migration operation.

    If you want to migration special version, there are two way to achieve it.

    1. you could write a button event and use the dbmigrator class from within your code. for more information, please refer to:

    https://romiller.com/2012/02/09/running-scripting-migrations-from-code/

    2. use migrate.exe which is handy for running them in a build step. for more information, please refer to:

    https://msdn.microsoft.com/en-us/data/jj618307.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by rbojanssen Thursday, March 29, 2018 6:57 AM
    Thursday, March 22, 2018 2:05 AM
    Moderator
  • Hello,

    It will depend on your target environment (client windows version and what tools are installed).  Here are some starting points to investigate:

    Using migrate.exe

    https://msdn.microsoft.com/en-us/data/jj618307.aspx

    Using EF Core

    http://www.bricelam.net/2014/09/14/migrations-on-k.html


    Cheers, Jeff

    • Marked as answer by rbojanssen Thursday, March 29, 2018 6:57 AM
    Sunday, March 25, 2018 8:05 PM

All replies

  • Hello,

    This really depends on what you are trying to solve.  Typically a database is a shared resource. For example, a single database server that multiple clients will connect to.  Migrations are a good idea but the issue you will have is how to handle versioning. 

    For example, with a hosted web application you have control as to when everyone receives the latest version (taking out a load balanced situation for simplicity).  In your situation, how will you control when all the desktop applications are updated?  With EF and migrations they all will need to be the same version.  In the database there is a table that maintains the current version and at runtime this is compared with the client.  If they do not match then an error is thrown.

    If everyone has their own copy of a database, then this problem goes away but this typically is not done.

    Hope this helps.


    Cheers, Jeff

    Wednesday, March 21, 2018 10:02 PM
  • Hi rbojanssen,

    Thank you for posting here.

    According to your question is more related to EF, I will move it to ADO.NET Entity Framework and LINQ to Entities forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 22, 2018 1:10 AM
  • Hi rbojanssen,

    >>But in the future the app will be running on client computers. How do I update the database on the client computer? Can I run the migrations from within the application exe file? 

    If dev machine and client machine use different MSSQL. A simple way is that you backup database from your dev computer and restore on your client computer without any migration operation.

    If you want to migration special version, there are two way to achieve it.

    1. you could write a button event and use the dbmigrator class from within your code. for more information, please refer to:

    https://romiller.com/2012/02/09/running-scripting-migrations-from-code/

    2. use migrate.exe which is handy for running them in a build step. for more information, please refer to:

    https://msdn.microsoft.com/en-us/data/jj618307.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by rbojanssen Thursday, March 29, 2018 6:57 AM
    Thursday, March 22, 2018 2:05 AM
    Moderator
  • Thx Jeff for the reply,

    Some more info: It will be a desktop app with a local database (MSSQL). Local being on a computer with the client not in the cloud. Possible there is one server computer and more client computers but even then, I do the support of the application so if the app gets an update, all computers of that client wil receive the update.

    I am looking for an easy way to update the database of the client when I change my model. On my dev computer I just run the "update-database" command in the console and I'm done. The ideal situation for me would be if I can run that "update-database" command from within the exe on the client computer (or some other easy way to migrate to latest db version without loosing data). 

    I would like to be able to do it from within the exe because I wrote an updater that installs the new exe on the client computer when I place an update online.

    Thursday, March 22, 2018 9:59 AM
  • Thx for moving and apologies for posting in the wrong forum.
    Thursday, March 22, 2018 9:59 AM
  • Thx for the reply Zhanglong!

    The application is installed on client computers as is the database (possible one server and more clients but that makes no difference here). 

    I want to be able to update client database to latest version (without loosing data) from within the (new) exe file. That way when I added things to the model (new functionality) and provide the client with the new exe (wrote an updater for that), when he runs that exe for the first time it will update the database to latest version.

    I think (not sure yet, have to study it more) the first link is exactly what I need.

    Thursday, March 22, 2018 10:05 AM
  • My suggestion is to keep it simple and if you are the administrator that controls both the database and the distribution of the client applications, then I would continue to use the update-database to update the database.  You can alter the connection string as follows:

    Update-Database -projectName ProjectWithMigrations -startupProjectName ProjectWithMigrations -connectionString "Data Source=theipaddress;Initial Catalog=databasename;User ID=sa;Password=supersecretpassword;Persist Security Info=True;MultipleActiveResultSets=true" -connectionProviderName "System.Data.SqlClient"


    Cheers, Jeff

    Thursday, March 22, 2018 8:11 PM
  • Thx again Jeff!

    This might gonna sound stupid but where do I execute that command? On my dev computer I have Visual Studio and I execute it in the console. But on the client? Is there a command line for that?

    Saturday, March 24, 2018 8:46 AM
  • Hello,

    It will depend on your target environment (client windows version and what tools are installed).  Here are some starting points to investigate:

    Using migrate.exe

    https://msdn.microsoft.com/en-us/data/jj618307.aspx

    Using EF Core

    http://www.bricelam.net/2014/09/14/migrations-on-k.html


    Cheers, Jeff

    • Marked as answer by rbojanssen Thursday, March 29, 2018 6:57 AM
    Sunday, March 25, 2018 8:05 PM