Change Entity Framework database schema map after using code first

旧时模样 提交于 2019-12-02 11:47:17

问题


I've finished building my blog using EF and Code First.

EF was running against my local SQL Express instance, with [DBO] schema.

Now i want to publish the blog, and i have done the following :

  1. Generetade the scripts for the tables and all objects from SQL Express and change [dbo] to my [administrator] schema from my server.
  2. Ran the scripts against the server. No issues, all objects were created an populated just fine.
  3. I have modified Webconfig and added my BlogContext connection string to point to the server not local sql express.
  4. Published the site.

The error i am getting is : Invalid object name 'dbo.Articles'. - where Articles is one of my entities. It resides on my sql server, [Administrator].Articles.

As far as i can tell EF still thinks im using the DBO schema. Although i have added the connection string to point to administrator user.

How can i change the schema that EF thinks it should use?


回答1:


EF will use dbo schema if you didn't configure the schema explicitly through data annotations or fluent API.

[Table("MyTable", "MySchema")]
public class MyEntity
{

}

Or

modelBuidler.Entity<MyEntity>().ToTable("MyTable", "MySchema");



回答2:


Just for searchers: I am just working with EF5 .NET4.5, and

[Table("MyTable", "MySchema")]

does not work. Even if VS2012 shows there is an overload which takes 2 parameters, on build it gives the error: 'System.ComponentModel.DataAnnotations.Schema.TableAttribute' does not contain a constructor that takes 2 arguments.

But the code mapping works just fine.



来源:https://stackoverflow.com/questions/9341922/change-entity-framework-database-schema-map-after-using-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!