开发者

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 f开发者_如何学Pythonor 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


Specify schema explicitly:

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

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


To specify schema in fluent

protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<ClassName>().ToTable("TableName", "SchemaName");


I ran into this issue recently as well as we support several different schemas with the same model. What I basically came up with was the passing the schema name to the classes/methods that map the model. So for example, EntityTypeConfiguration subclasses take the schema name as a constructor argument, and pass it along with the hard-coded string to ToTable().

See here for a more detailed explanation: https://stackoverflow.com/a/14782001/243607

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜