Multiple database with the different (database) structure asp.net mvc - entity-framework

I am using MVC 5 with multiple existing database using Entity Framework 6.
The structure is almost the same in each one, except in some case there could be an additional column in a table
Example:
Database_A has table Table_A with Col_1 and Col_2.
Database_B has a newer version of data base structure and has Table_A with Col_1, Col_2 and Col_3
I know how to switch from one database to another but my problem is: How I can make my model that covers both (or more) databases structure?

If you are using entity framework and are writing code that only deals with the columns common to both databases you should have no problem. When you create your db context, just pass it the connection string for the appropriate database and the code should work fine.
Gotchas to be aware of:
1. Creating new records where Col_3 is required. Make sure to have a default value.
2. Using migrations so Entity Framework will try to keep the database matching the model.

Related

EF does not generate every model from database using SQLite

I am trying to change the service based database in my project to SQLite. I am using EF database first approach but the EF does not generate every model from database. Eventhough foreign keys are set it does not generate the connections and also returns an error:
> Error 6005: The data type '' is currently not supported for the target Entity Framework version; the column 'Id' in the table 'main.Comments' was excluded.
It does so with every table that has an Id column (integer, primary key).
How can I fix this?
Thanks

Create view with jpa eclipselink

I'm working with two databases and I want a column in table1 of the first database to make reference
to a row in table2 in the second database. I'm asking if I could create a view to select columns from
database1 and database2, is it possible?
Have you tried the sample of the Eclipse site?
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Composite
With composite pattern you can use Two or more persistence units combined into a single persistence Unit.

Entity Framework Generate Database Schema (SQL) with Default Table Values

I am using EF 5 and SQL Server 2005, Model First (sort of).
By sort of, I mean that I typically build my schema in the SQL Server designer, but import the schema into EF so I have a visual view. There is often round-tripping.
However, I noticed that when I try to generate the DB schema based on the EF model, it skips all of the NEWID() default values that I have assigned as default values to my Guid IDs, but it doesn't skip the identity fields of type int.
I found this post explaining the reasoning for this:
Entity Framework 4 and Default Values
However, it doesn't answer my question: How do I get Entity Framework to generate a SQL DDL database schema with default values of NEWID() for my uniqueidentifier types?
NOTE:
I don't care about how to set them from the POCO entities and so forth (there are plenty of posts describing that) - my concern is getting the SQL DDL generated right so I can seed the database without worrying about these values going missing.
Using Entity Framework Migrations, you can use the GUID column builder and its DefaultValueSql parameter. The value of that parameter can be the string "NEWID()". This should take care of proper DDL generation.
Next you should declare these properties as database-generated using attributes or the fluent model builder, so that EF ignores the values set in your POCOs (which will be null for new objects).

entity framework map one model to many table

I store data in table named by month,such as data201201,data201202 and so on,table is created in the first day of month,all tables have same struct.I design this because each month ten million rows increase and I have only sqlserver standard version so Partition table can not be used.
Before use entity framework,i used sql string to query data,so I can change tablename in sql string by query condition.
Is these any way that I can query related table in Entity Framework?
I have two idea:
1.another table design that can support such big data and minor cost?
2.Intercept ef's query and change table name by query condition,and continue this query,but how and where i can do so?

Entity Framework 4 - Use a single mapping table for all many-to-many relations

I would like to map all many-to-may relations through a single table in my database.
Meaning that I have numerous tables (entities) that have various many-to-many relations. Instead of having a separate mapping table for every relation I would like to use one "master mapping" table having to columns: End1Id & End2Id.
Don't ask why ;) It's required by my customer...
How would I set this up in the model designer, or do I have to edit the edmx xml directly....or is it just not possible?
Thanx for your help!
In such a scenario you can't have explicit foreign keys, because a table like this normally has at least three rows:
PK of table 1
PK of table 2
Type of mapping, which specifies the exact tables to use.
Because of that, you can just create a table in EF, but it will also have no connections to other tables and you will have to do the joins manually.
You would need to set this Master Mappings table manually. The designer doesn't do it for you automatically.
However - if denormalized entities are what you are looking for, better have those denormalized in DB level rather than in EF/code level.