Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Ignore Schema

I am using Entity Framework 6 Code First to connect to an oracle database. EF is using "dbo" as the schema. I would rather not specify the schema, but rather let Oracle resolve the schema from the connection string.

Is there a way to omit "dbo" or any schema from the queries?

Example Given: Instead of "select * from dbo.table" I would like to see "select * from table".

Currently, I am parsing the connection string for the userId and using that as the schema, but I'd prefer to not use this technique.

like image 842
Cirem Avatar asked May 28 '14 14:05

Cirem


People also ask

What is schema in Entity Framework?

Schema. The default schema that EF Core uses to create database objects is dbo . You can change this behaviour using the ModelBuilder 's HasDefaultSchema method: protected override void OnModelCreating(ModelBuilder modelBuilder)

What is not mapped in C#?

The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database. In the following example, the AuditLog class will not be mapped to a table in the database: public class Contact.

Which of the following configures the default schema in Entity Framework Core?

HasDefaultSchema(ModelBuilder, String) Configures the default schema that database objects should be created in, if no schema is explicitly configured.


2 Answers

FrankO's answer is correct in that you can specify the default schema using the Fluent API.

In addition, this same method, in my experience, can be utilized to remove the default schema, allowing Oracle to resolve the schema in the case that it is specified on the connect string or if your companies policy is to access all tables through a public synonym.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema(string.Empty);
}

This would produce SQL containing FROM "MY_TABLE" instead of FROM "dbo"."MY_TABLE"

I'm using:

  • EntityFramework 6.1.3
  • Oracle.ManagedDataAccess 12.1.2400
  • Oracle.ManagedDataAccess.EntityFramework 12.1.2400
like image 69
Benjamin Brandt Avatar answered Oct 05 '22 11:10

Benjamin Brandt


You could use Fluent API to declare the default schema like the following:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.HasDefaultSchema("your_schema_here");
}
like image 22
FrankO Avatar answered Oct 05 '22 10:10

FrankO