Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finalize EF Migration strategy #54

Closed
rayrfan opened this issue Oct 24, 2017 · 1 comment
Closed

Finalize EF Migration strategy #54

rayrfan opened this issue Oct 24, 2017 · 1 comment
Assignees
Labels
Data Access repositories and data access related DB Change db schema or sql upgrade script changes
Milestone

Comments

@rayrfan
Copy link
Owner

rayrfan commented Oct 24, 2017

Dropping SQLite support

A hard decision but one worth making. I've supported Sqlite throughout v1 alpha and betas, the benefits of using Sqlite include:

  1. out of box user can just run the app, there is no db requirement whatsoever on users part, it just works.
  2. it works out of box on all the OSes, Win, Linux and Mac.
  3. for user running just a personal blog sqlite is sufficient, no need to spend extra on sql server.

However, supporting both Sqlite and SqlServer means I cannot easily maintain migrations in my code, because there has to be one set of migrations to keep for each db vendor I support, this is messy.

Coming back to supporting only Sql Server, means user needs to have Sql Server pre-installed on their machine, and possibly adjust the connection string, before running the app. This is a minor inconvenience compared to having migrations in my code, benefits include:

  1. easy upgrade, user can just run the new version of the app and all new db changes are applied through migration automatically
  2. if other decides to add a new app or plugin, they can add their migrations along, user running that code base will automatically have the necessary db tables etc ready for the new app or plugins.
  3. one less db vendor to support; sqlite migration has limitations, e.g. rename a column, which I don't have to live with

How to support multiple DbContext

So far I'm able to use multiple dbcontextes, one for Fan named CoreDbContext and one for Fan.Blogs named BlogDbContext, however I have to carry an over-all dbcontext FanDbContext in Fan.Web which is needed to do migration in startup.

Every time I add a new app or plugin I have to update startup.cs, this is not really convenient, I need to find a way to use reflection and do this part automatically.

@rayrfan rayrfan added the Data Access repositories and data access related label Oct 24, 2017
@rayrfan rayrfan added this to the 1.0.0-rc milestone Oct 24, 2017
@rayrfan rayrfan self-assigned this Oct 24, 2017
@rayrfan rayrfan added the DB Change db schema or sql upgrade script changes label Oct 24, 2017
@rayrfan rayrfan changed the title Drop SQLite support and finalize EF Migration strategy Finalize EF Migration strategy Oct 26, 2017
rayrfan pushed a commit that referenced this issue Oct 27, 2017
@rayrfan
Copy link
Owner Author

rayrfan commented Oct 27, 2017

I have dropped support for sqlite, I can concentrate on development more, don't have to switch between sql server and sqlite to test both.

I have come back to a single dbcontext that controls every entity in the entire solution, but this time I used reflection to find and load these entities, mappings dynamically.

To add a migration, open Package Manager

  1. select "src\Fan" as the Default project
    Add-Migration FanSchemaV1
    This will add a new folder "Fan/Migrations" with a migration named FanSchemaV1

  2. ctrl + f5, the site is up running with db automatically created

  3. Add a property to Post class, say public int SortOrder { get; set; }
    Add-Migration FanSchemaV1_Add_SortOrder
    This will add a new migration.

  4. ctrl + f5, site is up running with Post table have new column SortOrder added automatically in db

Any changes to db can now be down with Add-Migration and just run the app. If I want to get the sql script running behind each migration, I can run Script-Migration [-Idempotent] to review.

@rayrfan rayrfan closed this as completed Oct 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Data Access repositories and data access related DB Change db schema or sql upgrade script changes
Projects
No open projects
Development

No branches or pull requests

1 participant