Entity Framework 6 with multiple providers - sql-server

I have an application which is using Entity Framework 6 in order to interact with a SQL Server database. I have several migrations files due to the evolution of the application.
Now, I'd like to add the possibility to choice between a SQL Server database and a PostgreSQL database with keeping the same entity model.
When I apply the migrations files on SQL Server, all is ok.
But when I apply the same migrations files on PostgreSQL, it says that the database does not match the entity model and I have to generate a new migration file.
If I generate a new migration file, I see that it want to remove the property unicode on all string columns. I think it's because on PostgreSQL the type varchar (character varying) is able to store unicode and non unicode characters.
If I apply this migration file on PostgreSQL, all is ok now. But if I apply it on SQL Server, it say that the database does not match the entity model => columns varchar has been replaced per nvarchar.
I thought that the migrations files was able to adapt to the provider but apparently not.
For information, the property unicode is manage like that in the onModelCreating method :
modelBuilder.Entity<MyTable>()
.Property(e => e.ColumnName)
.IsUnicode(false);
So, my question is, do I have to manage different migration files depending on provider, knowing that I want to keep the same entity model ? If yes, how can I do this ?
Thanks a lot for your help

I have found a solution, I'm not sure it's the best but it's working.
In order not to be annoy anymore, I've created a batch migrations files per provider.
For that I've created one configuration per provider with specifying a specific context key and a different folder containing the migrations files.
This permits to switch between the different configurations depending of the current provider.

Related

Entities with relationships transfering via serialization problem - different IDs Entity Framework Core , SQL

I am using EF core and SQL database in my project. I have data model with several entities (principal with child/dependent ones) and as key I used int IDs. My entities have also defined unique UUID property for other usage. One of the is to identify same entity in test/production environment because primary key is not consistent. That means entity on test server ID=1 is not the same entity on other SQL server with production environment.
I thought using INT ID would be a good performance choice BUT I came to a problem:
I am in need to transfer selected data (entities) from test to production SQL server. My idea was to serialize those entities to JSON via .NET core native serializer from test machine and loading them to production one.
I am able to identify individual entities between enviroments via UUID property, but the problem is with setting relations/navigation between them. Have anyone solved similar issues?

MVC Membership database to SQL Server 2008

When I create a new MVC application with EF, it creates all the views, models, controllers and logic for users to be able to log in, change passwords etc. The data is held in a MDF file in the app_data directory.
I used this for my users, and then had my own SQL Server 2008 database which was created code first using EF for all my other tables. When I wanted to reference a user from a table within this database, I used the ApplicationUser.Id.
Doing it this way, I have not got a foreign key between the table that holds the users and any other table in my custom DB, but thats a different topic.
The question I have, is how I can I stop this happening every time I create a new application, and how do I fix the issue I have now.
I have two different databases, when I just need one. have one database in SQL Server (which is what I want) and one in a file which I cannot convert to SQL Server as it was created in 2012, and I am using 2008 SQL Server.
I am wanting to now deploy this on a server where all data is read from SQL Server.
The way I see it, I need to somehow get all the tables it creates for Membership, and put them into the database I created, then change the connection string. However first off I don't know how I would do this, and second... WHY do I have to do this? How can I just have one database next time. Am I missing something?
After some research and messing around, I have answered my own question and think it may be useful for others.
I am using MVC 5 with the default ASP.NET Identity for my users.
What I found is that if I changed the connection string to point to my SQL server, it will create the database for me (the one that was previously a file in my app directory).
I then added a connection string to the same database for the context I created and it created all the tables for that in the same database.
The result is that I have one database with all my tables in.

Specify SQL Server File Locations when using Entity Framework Database.Create()

I have several Entity Framework Code First DbContext objects that use a custom Initializer.
In the initializer, the call to
context.Database.Create();
creates the database in SQL Server.
The Data and Log files are created in directories per the Database Settings in SQL Server.
I would like different DbContext subclasses to have different Data and Log file paths. Can I specify the paths somehow when creating the database, or must I detach/move/attach in a separate step after the database has been created?
You can always use the AttachDBFilename keyword in the connection string. See the documentation of SqlConnection.ConectionString for more information.

Entity Framework 4 column conversions - SQL Server - SQLite

I have a SQL Server database and Entity Framework Model setup with POCO objects, this is working fine. However, I have also created a Sqlite database from the SQL Server database (using a simple conversion process).
So I have created another EDMX file for the SQLite database, which I would like to use my existing POCOs generated from the SQL Server model and map them to the database. Unfortunately this maps the SQLite primary key integer columns as int64 (this seems to be the underlying integer type in SQLite), when they are simply ints in the SQL Server database.
This means when I try to map the new SQLite EDMX to my existing POCO objects (generated from the SQL Server EDMX by the POCO T4 Template), the objects cannot be mapped at runtime (and throw exceptions) because the id columns differ in type.
Does anyone know of a fix for this, for example is there a way to convert values in the entity framework edmx file? If there isn't I would consider this a fairly major limitation of the entity framework.
Thanks for any assistance.

How to partially migrate a database to a new system over time?

We are in the process of a multi-year project where we're building a new system and a new database to eventually replace the old system and database. The users are using the new and old systems as we're changing them.
The problem we keep running into is when an object in one system is dependent on an object in the other system. We've been using views, but have run into a limitation with one of the technologies (Entity Framework) and are considering other options.
The other option we're looking at right now is replication. My boss isn't excited about the extra maintenance that would cause. So, what other options are there for getting dependent data into the database that needs it?
Update:
The technologies we're using are SQL Server 2008 and Entity Framework. Both databases are within the same sql server instance so linked servers shouldn't be necessary.
The limitation we're facing with Entity Framework is we can't seem to create the relationships between the table-based-entities and the view-based-entities. No relationship can exist in the database between a view and a table, as far as I know, so the edmx diagram can't infer it. And I cannot seem to create the relationship manually without getting errors. It thinks all columns in the view are keys.
If I leave it that way I get an error like this for each column in the view:
Association End key property [...] is
not mapped.
If I try to change the "Entity Key" property to false on the columns that are not the key I get this error:
All the key properties of the
EntitySet [...] must be mapped to all
the key properties [...] of table
viewName.
According to this forum post it sounds like a limitation of the Entity Framework.
Update #2
I should also mention the main limitation of the Entity Framework is that it only supports one database at a time. So we need the old data to appear to be in the new database for the Entity Framework to see it. We only need read access of the old system data in the new system.
You can use linked server queries to leave the data where it is, but connect to it from the other db.
Depending on how up-to-date the data in each db needs to be & if one data source can remain read-only you can:
Use the Database Copy Wizard to create an SSIS package
that you can run periodically as a SQL Agent Task
Use snapshot replication
Create a custom BCP in/out process
to get the data to the other db
Use transactional replication, which
can be near-realtime.
If data needs to be read-write in both database then you can use:
transactional replication with
update subscriptions
merge replication
As you go down the list the amount of work involved in maintaining the solution increases. Using linked server queries will work best if its the right fit for what you're trying to achieve.
EDIT: If they're the same server then as suggested by another user you should be able to access the table with servername.databasename.schema.tablename Looks like it's an entity-framework issues & not a db issue.
I don't know about EntityToSql but I know in LinqToSql you can connect to multiple databases/servers in one .dbml if you prefix the tables with:
ServerName.DatabaseName.SchemaName.TableName
MyServer.MyOldDatabase.dbo.Customers
I have been able to click on a table in the .dbml and copy and paste it into the .dbml of the alternate project prefix the name and set up the relationships and it works... like I said this was in LinqToSql, though have not tried it with EntityToSql. I would give it shot before you go though all the work of replication and such.
If Linq-to-Entities cannot cross DB's then Replication or something that emulates it is the only thing that will work.
For performance purposes you probably want either Merge replication or Transactional with queued (not immediate) updating.
Thanks for the responses. We're going to try adding triggers to the old database tables to insert/update/delete records in the new tables of the new database. This way we can continue to use Entity Framework and also do any data transformations we need.
Once the UI functions move over to the new system for a particular feature, we'll remove the table from the old database and add a view to the old database with the same name that points to the new database table for backwards compatibility.
One thing that I realized needs to happen before we can do this is we have to search all our code and sql for ##Identity and replace it with scope_identity() so the triggers don't mess up the Ids in the old system.

Resources