Garrett Fogerlie
Garrett Fogerlie

Reputation: 4458

Access a table of a separate database using entity framework

I have an MVC website with its own database and everything is working fine. Now I want to access a table of a database from a different MVC site. I added the connection string in the Web.config and named it OldMvcDB. Then I added a class to access this table:

public class OldSiteDB : DbContext
{
    public OldSiteDB() : base("name=OldMvcDB") { }

    public DbSet<OldTable> OldTables { get; set; }
}

When I try to access this table, I get the error: The model backing the 'OldSiteDB' context has changed since the database was created.

This is because the old database has a lot of other tables so the context doesn't match.

How can I access this one table without having to duplicate all the items in my new site?

Upvotes: 1

Views: 51

Answers (1)

thepirat000
thepirat000

Reputation: 13124

You should add the following to your class constructor:

Database.SetInitializer<OldSiteDB>(null);

From this SO answer.

Upvotes: 1

Related Questions