D
D
Dmitry Petrov2012-07-05 16:45:35
.NET
Dmitry Petrov, 2012-07-05 16:45:35

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; }
    }
}


and
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; }
    }
}


I initialize the whole thing like this:
        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();

        }

and on list2 I get an error: "The model backing the 'TestContext2' context has changed since the database was created".
If I understand correctly, the problem is in the contexts. But what am I doing wrong?
And if I need to access my database from different assemblies ? I cannot define all classes and tables at once in one context, this is basically impossible for a distributed project.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
G
Gwynbleidd, 2012-07-05
@Sellec

Try to call
Database.SetInitializer(null);

S
SychevIgor, 2012-07-06
@SychevIgor

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…

D
Dmitry Petrov, 2012-07-06
@Sellec

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?

S
SkazochNik, 2014-02-22
@SkazochNik

livestreet.ru

T
tosteruser, 2014-02-22
@tosteruser

Joomla

D
Dmitry Mironov, 2014-02-22
@Dori-mori

WordPress or Dupal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question