Answer the question
In order to leave comments, you need to log in
Entity Framework DbContext
I started to deal with Entity Framework Code First, but I can’t understand something.
I create a WinForms Application project, add the EF 4.3.1 package, add a reference to System.Data.Entity. I create two classes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace WindowsFormsApplication4
{
class TestContext1: DbContext
{
public TestContext1()
: base("Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True")
{
}
public DbSet<TestContext1Class> Classes { get; set; }
}
class TestContext1Class
{
public int Id { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace WindowsFormsApplication4
{
class TestContext2: DbContext
{
public TestContext2()
: base("Data Source=localhost;Initial Catalog=TestDB;Integrated Security=True")
{
}
public DbSet<TestContext2Class> Classes { get; set; }
}
class TestContext2Class
{
public int Id { get; set; }
public string Name { get; set; }
}
}
private void button1_Click(object sender, EventArgs e)
{
var context1 = new TestContext1();
var context2 = new TestContext2();
context1.Database.Initialize(false);
var list1 = (from p in context1.Classes select p).ToList();
var list2 = (from p in context2.Classes select p).ToList();
}
Answer the question
In order to leave comments, you need to log in
Are you joking? Why do you need 2 contexts… make 1 context with 2dbset… there can be as many sets as you want in one context…
And how would you order to work with this context in a modular application? If I have, let's say, 3 different plugin assemblies, each one accesses its own table in the database, and how can I implement this with one context?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question