I am using two DB both mysql.
The default one is for storing the app data.
The second one is to read previously entered data from another application I built (PHP).
My config.models.migrate is set to 'alter'.
My model to read data has migrate: 'safe'.
In my mind it my model's migrate should've overwrite the models.js default migrate but it doesn't.
I don't want my non default table to be wiped out everytime sails lifts because that DB is from a PHP application which needs it.
I need this model just to retrieve data from it not write.
How can I prevent sails running the default auto migrate?
Currently it works if I set the config.models.migrate to safe but this is not OK.
I need though to allow sails alter the default DB;
So, default datastore alter yes, the second datastore alter no.
Appreciate your time to read this :-)
SailsV1 does not support setting the migration on each model. You can only set your migration default app wide.
Note the third bullet point:
https://sailsjs.com/documentation/concepts/models-and-orm/model-settings#?choosing-an-approach
Related
currently I am working at a project which requires to be backwards compatible with (non-EF) databases, but also want to create a new database from model.
For this task I save the current schema somewhere (in XML form) and update the databases with raw sql update steps, until they match the schema, which is working fine.
Also, the modelBuilder matches the schema (as in, my algorithm finds no difference between the newly created database by context.Database.Create() and my saved schema) currently.
Since the schema will most likely change in later stages of development, I do have to support two ways to create an Up-to-date database and was wondering if I could combine these two - since now I have to update the saved target schema, create the update steps AND update my modelBuilder so that is creates exactly the database I need - which would be quite a tedious task.
So since there is probably no way to "translate" my schema to modelBuilder entries and because there is more not mapped information in my POCO classes (which prohibits the approach of updating a correct database and update my classes database first) the only (visible to me) way would be to somehow gather the CREATE TABLE statements a context would create when I call Database.Create() which I can use to update my schema and the update steps accordingly.
I know quite sure I can do the same by logging the context while calling the Create() method, however - this will take quite some time, will issue some queries I do not need and will create a dump database I have to get rid of afterwards each time I update my model.
So I was wondering if there was a way to inspect the modelBuilder (or the context, of course) and somehow see what the tables would look like it maps to.
I'm a newbie when it comes to EF. Always used NHibernate and now I'm learning something new.
I've got a project in which everything was created using database first approach.
Is there a way to use Code first to go along with the database first ?
So that any changes to database will be visible in code but also so it would be possible to make changes to database by changing existing/ creating new models?
Cheers!
You can use Code First to an Existing Database approach and for keep Database with models in sync and you can apply Code First Migrations with an existing database and for more info see channel9 video:
Migrations - Existing Databases
You can use Code First From Existing Database to generate Code First from a database. This will most probably mean you will need to change to Code First for your DataContext to keep it in sync in code going forward.
http://msdn.microsoft.com/en-us/data/jj200620.aspx
If I have a Grails application which I have used to persist some domain objects to my database (MongoDb in this case but probably does not matter), and I modify my domain class, say I add some more properties or take some properties away. Now, will the modified version of the application with the newer version of the domain class still recognize the already persisted old version of the domain instances?
More importantly if I now restart the Grails application with the new version of the domain class, will it delete(or do anything to) the already persisted old versions of the domain objects? I am trying to chase a similar issue I am facing but I am on a team so I'm not sure if the application deleted the old objects from the db (which I don't think so) or did some human system user.
The way in which Grails handles the database depends on the value of the dbCreate property in DataSource.groovy.
If you declare
dataSource {
dbCreate = "update"
}
then any additional properties you add to your domain class will be added as columns in your database. Note that this will not delete any columns that already exist. So say you had a property called x and wanted to replace it with a property called y, this would create a y column in the database but the x column would still remain.
Here a list of the possible values and behaviors for dbCreate
create - Drops the existing schema. Creates the schema on startup, dropping existing tables, indexes, etc. first.
create-drop - Same as create, but also drops the tables when the application shuts down cleanly.
update - Creates missing tables and indexes, and updates the current schema without dropping any tables or data. Note that this can't properly handle many schema changes like column renames (you're left with the old column containing the existing data).
validate - Makes no changes to your database. Compares the configuration with the existing database schema and reports warnings.
any other value - does nothing
Hope this helps
I have written an application using EF 6.0 in combination with an SQL Server Compact 4.0 Database. When a customer uses this application for the first time, it (the application) should create a database-file in a given path with some initital values. Also migrations should be allowed, for it is quite possible that the object model might change with future versions of the app.
Now I´m wondering what would be the best way to to deploy the DB on the users productive system. I could think of three ways:
I could create a DB-file with initial values and just copy it to the right place during installation process and use MigrateDatabaseToLatestVersionInitializer in the app.
In the DbContext-Constructors (I have two contexts) I could check for an existing DB-file and use different Database-Initializers accordingly. Like a CreateDatabaseIfNotExistsInitializer with a seed method that creates initial data if no fiel is found and a MigrateDatabaseToLatestVersionInitializer if the DB-file exists.
I could use the MigrateDatabaseToLatestVersionInitializer always and in its "Seed"-method check for existing table entries and create them if they are not present.
Which of these ways is to be preferred or is there a better way I didn´t think of?
It sounds like this is a desktop application so you might want to catch permissions errors about creating the database file at installation time (i.e. option 1) rather than run time, especially as in option 2 the database initialization is not an imperative command you're giving that you can put a try...catch around.
I don't think option 3 would work as the Seed method gets run after all the migrations, so surely the migrations will either have successfully run, in which case the tables don't need creating, or they will have failed as the DB doesn't exist and therefore your Seed method won't get run.
I'm using EF 4 and MVC in C#,
When my application loads, I would like load create some entities to be added to my database, so where is the best place to add thsi functionality using EF? Global.asax on Start application?
What is a reasonable name convention for the class... BootStrap?
Thanks
If you have existing database you should not include the initialization into your application. The only way how to make this work in existing database is to execute some initialization in Application_Start. The initialization must check existence of every entity you want to insert and insert data only if the entity is not present. Because your database already exists, the initialization logic will have to run every time you restart the application. To avoid this you would also need some flag in the database to mark that initialization was already done (one of inserted entity can be considered as a "flag" but only if the application cannot remove this entity).
EF normally seeds data only when creating database or after database migration.
Edit: If you are creating test data on your test database you should be happy with database recreation each time your model changes (or with migrations) and custom database initializer to seed your data.