Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify an SQL username other than dbo in Code First Entity Framework ( C# ASP.NET MVC 3 )

I'm trying to connect to an SQL Server 2008 database in a shared hosting environment from C# from within an ASP.NET MVC 3 application connecting via EF (code first).

My problem is that the generated SELECT statement looks like so:

SELECT ... FROM [dbo].[TableName]

which throws the error Invalid object name, but works fine when I do:

SELECT ... FROM [mySQLUserName].[TableName]

How do I specify a username other than dbo (e.g. mySQLUserName)?


EDIT:

The closest articles I have found that are relevant to this issue are:

  • http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
  • http://weblogs.asp.net/scottgu/archive/2010/07/23/entity-framework-4-code-first-custom-database-schema-mapping.aspx

with specific emphasis on the second article, however it doesn't specify how to set a username other than dbo

like image 439
Ryan Kirkman Avatar asked Aug 25 '11 02:08

Ryan Kirkman


People also ask

How do I set schema name in Entity Framework?

As with any code-first schema customization, you can do this by using the entity classes' attributes or through the DbModelBuilder API. With data annotations, you can use the optional second parameter of the Table attribute to specify the schema name. The code in Figure 3 implements this change in the model.

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…


1 Answers

You can specify the schema using a property on the TableAttribute that decorates your entity classes.

[Table("TableName", Schema = "mySQLUserName")]
like image 130
Adam Maras Avatar answered Sep 19 '22 15:09

Adam Maras