Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework code first and database user

We're running into a small problem deploying a web application to another environment. We created the application's db using Entity Framework Code First approach (db automatic created from Model). In this development environment, we are using integrated security and the tables are created under the dbo user. The tables are like
     [dbo].[myTable]

For our other environment, we are using username/password authentication for the DB. We scripted the tables and created them on the DB. So they are now named like
     [myDbUser].[myTable]

When running the application, we encounter always the problem
     Invalid object name 'dbo.myTable'.

Seems like the code is still trying to look for a dbo table, which is not present and thus fails.

Can anyone shed some light on this problem? Where does Entity Framework gets this dbo prefix from?

Thanks

like image 826
Ronald Avatar asked Jun 05 '11 19:06

Ronald


People also ask

Can we use code first with existing database?

This step-by-step walkthrough provides an introduction to Code First development targeting an existing database. Code First allows you to define your model using C# or VB.Net classes. Optionally additional configuration can be performed using attributes on your classes and properties or by using a fluent API.

Which is better code first or database first in Entity Framework?

Versioning databases is hard, but with code first and code first migrations, it's much more effective. Because your database schema is fully based on your code models, by version controlling your source code you're helping to version your database.


2 Answers

Specify schema explicitly:

[Table("Users", Schema = "dbo")]
public class User { .. }

Or specify default db schema for your user - 'dbo'

like image 66
Konstantin Tarkus Avatar answered Oct 08 '22 10:10

Konstantin Tarkus


To specify schema in fluent

protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<ClassName>().ToTable("TableName", "SchemaName");
like image 21
hkutluay Avatar answered Oct 08 '22 10:10

hkutluay