Reputation: 552
I'm working on an occasionally connected application with the Entity Framework. I have an online database (SQL Server) and a local database (SQL Server compact 3.5). I am using Microsoft Sync Framework to synchronize some of my tables from the online database to the offline database. I did the provisionning on each databases. What I want to do is to use the same model that I get from the reverse engineering I did on my online database for the local database.
I'll make a simple example to illustrate where I am at. It's not the databases I'm working on. It is just an example.
My online database contains one table Product. I want to occasionally synchronize that database with a local database. For that, I use Microsoft Sync Framework. No problem, I can make it works. So now, I have my two databases containing the same Product table (plus some of the synchronizing tables...).
In my WPF project, I use reverse engineering on the online database to get the model. So in my project, I have now the Product class, and the onlineDB context.
public partial class Product
{
public int id { get; set; }
public string name { get; set; }
}
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
// Primary Key
this.HasKey(t => t.id);
// Properties
this.Property(t => t.id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.name)
.IsFixedLength()
.HasMaxLength(10);
// Table & Column Mappings
this.ToTable("Products");
this.Property(t => t.id).HasColumnName("id");
this.Property(t => t.name).HasColumnName("name");
}
}
public partial class onlineDBContext : DbContext
{
static onlineDBContext()
{
Database.SetInitializer<onlineDBContext>(null);
}
public onlineDBContext()
: base("Name=onlineDBContext")
{
}
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ProductMap());
}
}
With this, I can correctly update, delete, insert... data to my onlineDatabase. No problem.
How can I use the same Product class to communicate with my local database (SQL Server Compact 3.5) ? Is it possible ? I guess I have to create another context but i'm not use to create it manually.
Upvotes: 0
Views: 862
Reputation: 136
As you can see in your code, constructor of DbContext
is passing nameOfConnectionString
to base class. In your case this string isonlineDBContext
which means that you have a connection string with that name in your .config file.
you can change your connection string in order to connect to different source. Find out more about how to do that here and here.
Upvotes: 1