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:
with specific emphasis on the second article, however it doesn't specify how to set a username other than dbo
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.
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…
You can specify the schema using a property on the TableAttribute
that decorates your entity classes.
[Table("TableName", Schema = "mySQLUserName")]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With