Ale Miralles
Ale Miralles

Reputation: 604

Querying multiple schemas using EF

I think this should be easy, but I cannot figure it out. I have an existing (legacy) database that I'm querying using EF, I have created a couple of POCO classes and as long as issue queries against the 'dbo' schema everything works great. The problem is I cannot issue queries against anyother schema, let say 'foo'. I tried overriding OnModelCreating and specifying the schema, but it doesn't seems to work... Does anybody knows a workaround for this? For this particular case I only need querying capabilities (not insert, update, etc..). If it is there anything that will works with queries only, it will be great too. I'm using EF 5 targeting .NET 4.0 Any help will be apreciated. Thanks!

Upvotes: 1

Views: 95

Answers (2)

phil soady
phil soady

Reputation: 11348

the ToTable method is overloaded with the option to pass the Schema

modelBuilder.Entity<Poco>().ToTable("tabX","schemaY");

Thats interesting to know that "schema.table" works, but since the overload is there, may be better to use it.

Upvotes: 1

Ale Miralles
Ale Miralles

Reputation: 604

Never mind, after a LOT of tries, I finally figured it out

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
//don't know why, but for some reason this doesnt work (from scott gu's blog)
//modelBuilder.Entity<Foo>().ToTable("tblFoo", "bar");                        
//but this line of code does the trick ;)
 modelBuilder.Entity<Foo>().ToTable("bar.tblFoo");

}

Upvotes: 0

Related Questions