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.
Related
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.
I made a custom application that is running from several years and is full of company data.
Now I need to replicate the application for another customer, so I set up a new server then i cloned the databases and empty all the tables.
Then I made a database and file shrink.
On the SQL Server side, the databases looks empty but if I run a grep search on the database files .mdf and .log I still can find recurrence of the previous company name also in system databases.
How do I really clean a SQL Server database?
Don't use backup/restore to clone a database for distribution to different clients. These commands copy data at the physical page/extent level, which may contain artifacts of deleted data, dropped objects, etc.
The best practice for this need is to create a new database with schema and system data from scratch using T-SQL scripts (ideally source controlled). If you don't already have these scripts, T-SQL scripts for schema/data can be generated from an existing database using the SMO API via .NET code or PowerShell. Here's the first answer I found with a search that uses the Microsoft.SqlServer.Management.SMO.Scripter class. Note you can include scripts data too (insert statements) by specifying the ScriptData scripting option for desired tables.
Due to an error in our build process, we had the following initial situation:
Connection string of the datasource
jdbc:jtds:sqlserver://db01.example.de/AppDB_Example_Prod
Entity files have the catalog "AppDB_Example"
A stored procedure that is called using a named query
{ CALL usp_performSearch(:searchQuery) }
As you can see, we have a missmatch in the connection string and the catalogs. Normally, they must/should be equal.
At runtime, we execute the stored procedure and retrieved the results from the database AppDB_Example_Prod, as this is the database we are connected to. After that, we load related entities using the entityManager from the database AppDB_Example, as this is the catalog mentioned in the annotation of the entity. JPA is doing this itself, we do not have any influence on this.
Searching through the internet, I've read that you should create multiple persistence units / data sources, to work with multiple databases.
Does it work, as it is supposed do do or did we hit a bug?
Could this be used without any problem to work with multiple database via one connection string?
Does this only work with SQLServer (MSSQL) and so it will fail if we may change to an other database in the future?
This feature isn't supported by JPA itself but depends on the database and the permissions of your connection (= usually the DB user which you use to connect).
JPA doesn't care much about the schema. If you don't specify one, then JPA will not send schema information to the database. Usually, there is a default schema attached to the user (or one is specified via the JDBC connection settings). That way, the database knows where to look.
If you specify a schema, then JPA will include this information in the SQL it sends to the database. That means instead of TABLE.COLUMN, it will generate SCHEMA.TABLE.COLUMN. Whether this works depends only on the database (and maybe the JDBC driver) but not on JPA.
All SQL databases should allow you to look at other schemas than the default one if your DB user has the necessary permissions.
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.
Suppose you have a single web portal application that is used by a number of different clients. For reasons of security and portability, each client's data must reside in a separate database. The schema for each of these databases is absolutely identical.
How does one go about accessing these separate databases from a single SQL Server, and how does one tell the Linq to SQL data classes which database to access?
All objects in the context are defined using two part names (schema.object) and at runtime you just create the context using a connection string pointing to the right database.
Just pass it the connection string to the DataContext constructor.
Example:
var dataContext = new FooDataContext ("SomeConnectionStringDependingOnWhichDataBaseToHit");
Every query that you generate based on that data context will hit the DB pointed on the connection string.
You can add as many Linq to Sql classes that you need.