I'm working with SQLite database using EF Code-First approach.
My first DbContext works with three physical databases (DataSource=@"D:\Users.db" and Attach=@"D:\UserTypes.db;D:\Messages.db").
I map different DbSets to different physical databases inside OnModelCreating event:
Code: Select all
...
public DbSet<Message> Messages { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserType> UserTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
var sb = new SQLiteConnectionStringBuilder(Database.Connection.ConnectionString);
var attach = sb.Attach.Split(';');
var firstAttachedDbSchemaName = Path.GetFileNameWithoutExtension(attach[0]);
var secondAttachedDbSchemaName = Path.GetFileNameWithoutExtension(attach[1]);
modelBuilder.Entity<UserType>().ToTable("UserType", firstAttachedDbSchemaName);
modelBuilder.Entity<Message>().ToTable("Message", secondAttachedDbSchemaName);
...
But when I'm trying to read some data via that second context I have an error:
Looks like second DbContext is trying to work with D:\Messages.db despite of connectionString - why?SQLite error
no such table: Messages.Message
Second stange thing is that OnModelCreating fires only once - when I open connection of my first DbContext.
P.S. I'm using dotConnect for SQLite 5.3.583.
I've uploaded sample project here. Click AddUserTypeUsersAndMessages() button to create databases (on disk D:\), CreateMessagesCopy() button to copy database and SelectSecondContextAndGetMessages() button to get an error.