Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Unassigned: Wrong/odd behavior using HasDefaultSchema() feature (EF6) [1253]

$
0
0
I have detected a odd behavior using HasDefaultSchema y my DbContext.
I have a DbContext that iherited from other.

The base context:
```
public class AppFwBaseContext : DbContext
{
public DbSet<Usuario> Usuarios { get; set; }
public DbSet<Perfil> Perfiles { get; set; }
public DbSet<Grupo> Grupos { get; set; }
public DbSet<Rol> Roles { get; set; }
public DbSet<MemberAccount> MemberAccounts { get; set; }
public DbSet<OAuthMembership> OAuthMemberships { get; set; }

public AppFwBaseContext(string connectionString) : base(connectionString) {}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("AppFw");
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new PerfilMapping());
modelBuilder.Configurations.Add(new UsuarioMapping());
modelBuilder.Configurations.Add(new GrupoMapping());
modelBuilder.Configurations.Add(new RolMapping());
modelBuilder.Configurations.Add(new MemberAccountMapping());
modelBuilder.Configurations.Add(new OAuthMembershipMapping());
}
}

```
On OnModelCreating I set the default schema to "AppFw".

The final context:
```
public class BikeOverflowDataContext : AppFwBaseContext
{
public DbSet<Bike> Bikes { get; set; }

public BikeOverflowDataContext(string connectionString) : base(connectionString) {}
public BikeOverflowDataContext() : this("DefaultConnection") {}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("BikeOverflow");
modelBuilder.Configurations.Add(new BikesMapping());
}

}
```
In this case, I only add a new DbSet "Bikes" and override OnModelCreating and after call to base.OnModelCreating I call SetDefaultSchema method to use "BikeOverflow" as schema.

Ok, run my application and using a DropCreateDatabaseAlways initializer the app create the Database. But, different as I expect, all tables go to "BikeOverflow" schema ...

I see It strange but I have some test and I discover a wrong behavior:

If I run this:
```
var users = (new BikeOverflowContext("DefaultConnection")).Usuarios; // Get all users using BikeOverflowContext
```
The generate SQL is:
```
SELECT
[Extent1].[Id] AS [Id],
'0X0X' AS [C1],
[Extent1].[Codigo] AS [Codigo],
[Extent1].[Nombre] AS [Nombre],
[Extent1].[Descripcion] AS [Descripcion],
[Extent1].[FechaCreacion] AS [FechaCreacion],
[Extent1].[Timestamp] AS [Timestamp],
[Extent1].[Email] AS [Email],
[Extent1].[UsuarioCreador_Id] AS [UsuarioCreador_Id]
FROM [BikeOverflow].[Perfil] AS [Extent1]
WHERE [Extent1].[Discriminator] = N'Usuario'
```
All is ok, and I get the list of entities "Usuario" from my database but ...

If I run:
```
var users = (new AppFwBaseContext("DefaultConnection")).Usuarios;
```
The generate SQL is:
```
SELECT
[Extent1].[Id] AS [Id],
'0X0X' AS [C1],
[Extent1].[Codigo] AS [Codigo],
[Extent1].[Nombre] AS [Nombre],
[Extent1].[Descripcion] AS [Descripcion],
[Extent1].[FechaCreacion] AS [FechaCreacion],
[Extent1].[Timestamp] AS [Timestamp],
[Extent1].[Email] AS [Email],
[Extent1].[UsuarioCreador_Id] AS [UsuarioCreador_Id]
FROM [AppFw].[Perfil] AS [Extent1]
WHERE [Extent1].[Discriminator] = N'Usuario'
```

And I can see that the app have created another time all tables in the "AppFw" schema, then I have duplicated all tables ...

I think that SetDefaultSchema should set the schema to entities that are in the present class and no affect to inherited DbSet's or enable some mechanism where I can set default schema for current DbSets or all dbset. For example:

public virtual void HasDefaultSchema(string schema); // To set schema for all dbsets
public virtual void HasDefaultPartialSchema(string schema); // To set schema for dbsets in this class

I hope it help you to solve this behavior.



Comments: Hello. First of all, sorry if my question should not be here. I'm working creating a custom DB Provider, and I'm trying to implement this feature but I'm facing some troubles. One of them: I'm overriding DBProviderServices class to create the DB and the tables using the right syntax, when I set the default schema I'm able to get it from the System.Data.Entity.Core.Metadata.Edm.Entity class, so when I create the tables all have the schema configured including Migration table. But if I perform a TEntity.ToList() or TEntity.Add(), the query is performed against the table entity but without using the schema configured its only contains the table name. So my question is: what class should I override to be able to send the queries with the right syntax? At this time I was not able to find a class that will give that ability, or find why the queries doesn't have the schema configured.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>