How does EF check if the database is latest version? - entity-framework-4.1

I know EF checks the EdmMetadata table to determine if the version of model classes is same as database tables.
I want to know exactly how EF can find if the version of a model has changed. In other words, I want to know what does EF compare to the modelhash in the database?

Have a look at this blog post about the EdmMetadata table.
For your question, this is the relevant parts:
The EdmMetadata table is a simple way for Code First to tell if the
model used to create a database is the same model that is now being
used to access the database. As of EF 4.1 the only thing stored in the
table is a single row containing a hash of the SSDL part of the model
used to create the database.
(Geek details: when you look in an EDMX file, the SSDL is the part of
that file that represents the database (store) schema. This means that
the EdmMetadata model hash only changes if the database schema that
would be generated changes; changes to the conceptual model (CSDL) or
the mapping between the conceptual model and the database (MSL) will
not affect the hash.)

Related

web2py dal howto define model at runtime

I am a newbie to wewb2py,is it possible to create at runtime a model of a legacy database, for using DAL with it? I saw that there are some scripts that create the model file, but I do not know whether it is correct to put this file in the model directory of my application, I think not, I did some experiments, I can connect to the database with DAL querying its tables and for every table I can get the definition of the fields, the I tried to define the table with define_table,it works but try to create the table on the database and return an error because the table already exists; this is the relevant part of my code:
conn_string = "mysql://{0}:{1}#{2}/{3}".format(user,pwd,host,db_name)
db = DAL(conn_string)
db.define_table('test1',Field('prova','string'))
it works only the first time, when the table test1 does not exist yet on the database, I do not need to create the tables only work with their data, can you put me on the right way?
db = DAL(conn_string, migrate_enabled=False)
The above will prevent web2py from doing any migrations, including attempting to create any tables.

Mondrian - Fact Table Data as XML

I am evaluating a Mondrian-Saiku solution for a client.
After analyzing their current database schemas, I realize that what constitutes as their 'fact table data' is currently being stored in XML's. The XML 's themselves are stored as blob datatypes in a MySQL table. Think of it like this: the table holds all the transactions of the company; the details of each transaction are stored in their own XML; each XML string is stored as one of the field values in a given transaction row.
This presents a slight dilemma since the Mondrian XML schema requires the explicit use of column names.
Short of having to extract and transfer the XML data to new tables (not realistic for my purposes due to the size of data and dependencies from other systems), is there any way I can work my client's existing setup for the purposes of a Mondrian-Saiku implementation?
You need to expose the data in a traditional table way. What is the database here? Can you create a database view which does some xml processing on the XML in the blob and exposes the columns?
Alternatively maybe something like composite or jboss teiid can help here. These tools allow you to expose as a standard looking table, virtually anything. It may not be quick enough though!

Entity Framework 4.1 Custom Database Initializer strategy

I would like to implement a custom database initialization strategy so that I can:
generate the database if not exists
if model change create only new tables
if model change create only new fields without dropping the table and losing the data.
Thanks in advance
You need to implement IDatabaseInitializer interface.
Eg
public class MyInitializer : IDatabaseInitializer<MyDbContext>
{
public void InitializeDatabase(MyDbContext context)
{
//your logic here
}
}
And then set your initializer at your application startup
Database.SetInitializer<ProductCatalog>(new MyInitializer());
Here's an example
You will have to manually execute commands to alter the database.
context.ObjectContext.ExecuteStoreCommand("ALTER TABLE dbo.MyTable ADD NewColumn VARCHAR(20) NULL");
You can use a tool like SQL Compare to script changes.
There is a reason why this doesn't exist yet. It is very complex and moreover IDatabaseInitializer interface is not very prepared for such that (there is no way to make such initialization database agnostic). Your question is "too broad" to be answered to your satisfaction. With your reaction to #Eranga's correct answer you simply expect that somebody will tell you step by step how to do that but we will not - that would mean we will write the initializer for you.
What you need to do what you want?
You must have very good knowledge of SQL Server. You must know how does SQL server store information about database, tables, columns and relations = you must understand sys views and you must know how to query them to get data about current database structure.
You must have very good knowledge of EF. You must know how does EF store mapping information. You must be able to explore metadata get information about expected tables, columns and relations.
Once you have old database description and new database description you must be able to write a code which will correctly explore changes and create SQL DDL commands for changing your database. Even this look like the simplest part of the whole process this is actually the hardest one because there are many other internal rules in SQL server which cannot be violated by your commands. Sometimes you really need to drop table to make your changes and if you don't want to lose data you must first push them to temporary table and after recreating table you must push them back. Sometimes you are doing changes in constraints which can require temporarily turning constrains off, etc. There is good reason why tools which do this on SQL level (comparing two databases) are probably all commercial.
Even ADO.NET team doesn't implemented this and they will not implement it in the future. Instead they are working on something called migrations.
Edit:
That is true that ObjectContext can return you script for database creation - that is exactly what default initializers are using. But how it could help you? Are you going to parse that script to see what changed? Are you going to execute that script in another connection to use the same code as for current database to see its structure?
Yes you can create a new database, move data from the old database to a new one, delete the old one and rename a new one but that is the most stupid solution you can ever imagine and no database administrator will ever allow that. Even this solution still requires analysis of changes to create correct data transfer scripts.
Automatic upgrade is a wrong way. You should always prepare upgrade script manually with help of some tools, test it and after that execute it manually or as part of some installation script / package. You must also backup your database before you are going to do any changes.
The best way to achieve this is probably with migrations:
http://nuget.org/List/Packages/EntityFramework.SqlMigrations
Good blog posts here and here.

Entity Framework expose several databases as a whole

I have three databases with exactly the same schema (SAP Business One databases). In this databases I have an item masters table connected to a warehouse stock table via the item code. Can I have just one Entity framework model that has only one item master object and one warehouse stocks object which draws data from the 3 databases?
The items are the same in the three databases but they have different warehouse codes.
I don't know if I have made myself clear.
If you want single EF model which will simultaneously load data from three databases then answer is no. If you want single EF model which can be used for all three databases the answer is yes but all your databases must use same database provider (server) and must have exactly the same schema of mapped tables.
The whole magic in this case is in connection string which can connect only to single database and cross database calls are not allowed.
If you need the first scenario you can try to hide unions and cross database queries in views and map those views in your model. This have two disadvantages:
Relation between views are not allowed in SQL Server but you can create the relation in EF model
Views are read only in EF model. If you want to modify data the best way is mapping stored procedures which will do that.

LINQ to SQL - Two tables, same name? or Alternate DB definition?

I need to update a C# application that imports data into a database using LINQ. I am new to LINQ. The problem I am trying to solve is that there are two versions of the DB. They have the same table names and are 90% identical in structure, but have one table (out of about 60) which has a different definition.
If LINQ were not involved, I would simply select a different query depending on which version of the application (DB) the user wanted to import to, and leave the remainder of the application as is.
My impression is that LINQ is intended for situations in which the DB structure is cast in stone, and that I cannot have two LINQ table definitions having the same name and simply or easily switch between them (or do so at all).
In this case, must I have (at least) a separate entire Linq.DataContext for each version of the DB? Or have I misunderstood something basic here?
You might be able to make that happen using separate mappings. In this case you would have to hand code your mappings as apposed to the attribute-based mapping that the LINQ designer or SqlMetal does for you. I've never done it, but I think it might work. I just googled for "Linq to Sql POCO mapping" and found this: Achieving POCO s in Linq to SQL. This person is loading his mapping from an xml file at runtime. You could conditionally load one of two different mapping files.