Entity Framework DataBase Initializors Add Columns - entity-framework-4.1

Are there any implemenations for DataBase Initializeers in EntityFramework, which adds at least new columns if missing ?
Sounds like a real common scenario to me so i don't want to reinvent the wheel

If you mean the scenario where you already have the database and you want update it from your modified model definition (code-first with data annotations or fluent mapping) than the answer is no. There are no initializers which would be able to modify existing database and there will not most probably be any in the future.
ADO.NET decided to use another approach for database upgrade which is called migrations. First public CTP of Migrations is already available.

Related

Targeting different database systems with EF6 (and Code First Migrations)

My project (WPF, .Net 4.5, EF6) has to target different DBMS, so far it's MSSQL, Oracle, MySql and Firebird.
I started with creating dbs-dependent scripts that generate the database(s) and then used Entity Frameworks Database-First-approach to create the models. By having a default edmx based on MSSQL and creating separate ssdls for the other providers (the differing ssdl-files can be configured in the connection strings) it all worked pretty good for all dbms.
The problem I see now is maintaining installation/updates for 4 or more different dbms for even many more customers. We won't send admins to install updates at our customers, we rather need something like a generic setup/update routine for all. It's possible but you would have to maintain different versions of sql scripts for each dbms and kind of a setup tool that handles those scripts and knows which to execute for which database (depending on dbms and current version).
When looking for alternatives I came across EF Code First Migrations and tried switching to this approach. My tries are based on MSSQL and MySql so far.
It all works good when I stick to either MSSQL or MySql. Creating migrations, applying them to existing or non-exisiting databases, all this works pretty fine.
But I'm stuck with bringing both system together. Applying MSSQL-based migrations to MySql for example seems to be impossible. The database will be created but it's not possible to connect due to type mismatches and so on.
My guess is that the "__Migration"-Table contains a model which was created based on MSSQL and is incompatible with the MySql-provider now. Just a theory but maybe someone knows better.
Does anyone know a solution for this? Is there any way to target different dbms with EF?
I can't believe that I am the only one with this problem but it's really hard to find any information about this.
Any help is appreciated, even directing me to other approaches than using EF that way or using EF at all.
I've got same situation, but I found simple workaround for that. Everything you need is just create seperate projects for each database system with enabled migration. In your case, two database contexts should inherit from one base context where all model builders and DbSets are kept.
Example model:
// MyModel.Base.csproj
public class Person { /*..*/ }
public class BaseDbContext : DbContext
{
public DbSet<Person> People { get; set; }
/*...*/
}
// MyModel.Sql.csproj
public class SqlDbContext : BaseDbContext {}
// MyModel.MySql.csproj
public class MySqlDbContext : BaseDbContext {}
Maybe having two migrations settings it's not so awesome idea, but you are able to use some database-specific scripts in your migrations.

How do I add a column to a table (schema upgrade) and have it map to an EF code first object

I have a database that I created for a site using Entity Framework 4.1 code first. I need to add a simple property to one of my entity classes, and add a corresponding (possibly nullable) column to the database without losing any of the data in the database.
I think I can take down the site, alter the database to add the column, and redeploy website with an updated entity class, but I'm not sure that's the best way to do this schema upgrade.
Is there a standard (or just better) way of doing a schema upgrade on a DB that was created using code first?
Entity Framework Code First has a new feature in preview called Code First Migrations, which does simple schema upgrades.
You can check it out here http://blogs.msdn.com/b/adonet/archive/2011/07/27/code-first-migrations-walkthrough-of-august-2011-ctp.aspx

Putting Rails over top of an existing database

I have an application written in PHP/MySQL (symfony, to be specific) that I'd (potentially) like to rewrite in Rails. I know how to create scaffolding for tables that don't exist yet, but how do I get Rails to read my existing table structure and create scaffolding based on that?
Update: it turns out I can run the following command to get Rails to generate models for me:
rails generate scaffold Bank --no-migration
But it doesn't give me forms. I would prefer something that gives me forms.
The answer is db:schema:dump.
http://guides.rubyonrails.org/migrations.html
The easiest route is to pretend that you are writing a fresh app with a similar database schema - you can then create your models and migrations with the old schema in mind, but without being restricted by it. At a later stage, you can create a database migration script to copy all the old data into the new schema.
I'm doing this right now. The benefit of this approach is that you can take advantage of all of the rapid development tools and techniques provided by Rails (including scaffolds) without being slowed by trying to retrofit to the exact same schema.
However, if you do decide that you don't like this approach, and you do need to map your new models to existing tables, there are a number of configuration options provided by active record where you can override the convention over configuration naming patterns and map model names to tables names, set oddly named ID fields etc. For example:
class Mammals < ActiveRecord::Base
set_table_name "tbl_Squirrels"
set_primary_key :squirrel_id
end
The above will help Rails attempt to read your existing table, but success will depend upon how well the existing table structures matches Rails conventions. You may have to supply more configuration information to get it to work, and even then it might not work.
Finally, it may be worth considering the use of DataMapper which I believe is more suited to existing brownfield databases than ActiveRecord, because it allows you to map everything, but of course you will need to learn that API if you don't already know it.

grails/gorm/mysql/hibernate

I have a simples question. I have been trying to learn Grails by my own, and i managed to do a simple application using Grails/Gorm.
1 ) Later, i decided to use Mysql instead of Gorm - i just needed to configure the 'DataSource' and download the driver.
2 )So if i want to use hibernate between both (Grails and MYSQL) like this:
http://www.grails.org/doc/latest/guide/15.%20Grails%20and%20Hibernate.html, i need to make an 'hibernate.cfg.xml' file, and specify my mysql database url, user, pw etc .. and i have to map each Class in Grails for MySql columns.
So what is the diference between 1) and 2) ? and what exactly hibernate does. Give examples if possible
PS. Please correct me if i said something wrong, im kinda new to this
I think you are a bit confused here.
GORM is not a database, it is a ORM that maps you Groovy classes to database tables. It uses Hibernate under the covers to achieve this (Hibernate is also an ORM).
The default database Grails uses is an in-memory HSQL DB. If you want to use MySQL instead of that, all you need to do is change the settings in conf/DataSource.groovy.
You don't need to create any Hibernate xml files. That part of the documentation you've linked to is to allow people with existing Hibernate domain models to easily re-use them.
Hope this helps clear things up.
cheers
Lee

Web application using Hibernate+MySql

i was asked to do a book manager at university with hibernate and mysql. I have a simple question. If i choose to do a web application, grails already uses hibernate. GORM runns over hibernate. so to use mysql i only need to configure jdbc grails drivers and that's it?
i mean, "for the project you must use hibernate and mysql" - this are the requirements. So can i do that way?
thanks in advance,
JM
Yes, of course you can.
You'll need to get the MySQL JDBC driver from this location.
Grails? When you're new to programming? Whose idea was this?
Personally, I think that taking on all these unknowns is risky for someone who's "new to programming." Do you know anything about SQL or JDBC? Have you ever written a web project before? This could be difficult.
I don't know how to be more specific. Download the JDBC JAR from the link I gave you.
I'd recommend that you start with a JDBC tutorial first. Hibernate is not for you - yet.
Hibernate is an object-relational mapping tool (ORM). It's a technology that lets you associate information in relational database tables to objects in your middle tier. So if you have a PERSON table with columns id, first, and last Hibernate will let you associate those data with the private data members in your Person Java class.
That sounds easy, but it gets complicated quickly. Your relational and object models might have one-to-many and many-to-many relationships; Hibernate can help with those. Lazy loading, caching, etc. can be managed by Hibernate.
But it comes at a cost. And it can be difficult if you aren't familiar with it.
If you have a deadline, I'd recommend creating a Java POJO interface for your persistence classes and doing the first implementation using JDBC. If you want to swap it out for Hibernate later on you can do it without affecting clients, but you'll have a chance of making progress without Hibernate that way.