Search found 5 matches

by francischie
Fri 25 Mar 2011 22:55
Forum: Entity Framework support
Topic: Code First Custom Convention for schema Naming
Replies: 23
Views: 12565

Thanks Andrey for the notes.

For the note 1, I didn't realized when I post the code that I'm using the ODP.Net beta provider (my dotConnect trial expired) which does not require to remove the System.Data.Entity.Infrastructure.IncludeMetadataConvention.

Is there are plan also for the dotConnect provider to omit declaring the removal of MetadataConvetion like the ODP.Net?
by francischie
Thu 24 Mar 2011 20:42
Forum: Entity Framework support
Topic: Code First Custom Convention for schema Naming
Replies: 23
Views: 12565

My alternative solution

Code: Select all

public abstract class DataContext : DbContext
  {
    public string DefaultSchema { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      if (!String.IsNullOrEmpty(DefaultSchema))
      {
        var entityMethod = modelBuilder.GetType().GetMethod("Entity");
        foreach (PropertyInfo dbSet in GetType().GetProperties().Where(t => t.PropertyType.IsGenericType && t.PropertyType.GetGenericTypeDefinition().Equals(typeof(DbSet))))
        {
          var entityType = dbSet.PropertyType.GetGenericArguments();
          var entityMethodGeneric = entityMethod.MakeGenericMethod(entityType);
          var entityConfig = entityMethodGeneric.Invoke(modelBuilder, null);
          var toTableMethod = entityConfig.GetType().GetMethod("ToTable", new Type[] { typeof(string), typeof(string) });
          var tableName = GetTableName(entityType.FirstOrDefault());
          toTableMethod.Invoke(entityConfig, new object[] { tableName, DefaultSchema });
        }
     
      }
      base.OnModelCreating(modelBuilder);
    }
      

    private string GetTableName(Type type)
    {
      var tableAttribute = type.GetCustomAttributes(false).OfType().FirstOrDefault();
      return tableAttribute == null ? type.Name : tableAttribute.Name;
    }

    #endregion
  }
Usage:

Code: Select all

public class SampleContext : DataContext
 {
    public DbSet Students { get; set; }
    public DbSet Classes { get; set; }

    public SampleContext ()
    {
      DefaultSchema = "your_namespace";
    }
 }
[/code]
by francischie
Fri 18 Mar 2011 17:49
Forum: Entity Framework support
Topic: Code First Custom Convention for schema Naming
Replies: 23
Views: 12565

Confirmed!

The IConfigurationConvention is dropped and the class ConventionsConfiguration does not have Add method anymore on EF 4.1.

Solution does not work anymore.
by francischie
Fri 04 Feb 2011 23:08
Forum: Entity Framework support
Topic: Code First Custom Convention for schema Naming
Replies: 23
Views: 12565

Sorry. Only the Fluent API is working not the DataAnnotation

Code: Select all

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) 
        { 
            modelBuilder.Entity().ToTable("CUSTOMER", "MYSCHEMA"); 
            modelBuilder.Conventions.Remove(); 

        } 
by francischie
Fri 04 Feb 2011 22:54
Forum: Entity Framework support
Topic: Code First Custom Convention for schema Naming
Replies: 23
Views: 12565

custom defaultSchemaconvention does not work for correctly

Hi,

I used the DataAnnotation on my POCO class but it's is not used to set the tablename, instead the value of tableName is the name of the Property of the DBContext of DBSet type.

Here's the example:

Code: Select all

 

 public class SampleDB : DbContext
    {
        public DbSet Customers { get; set; }

        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove();
            modelBuilder.Conventions.Add(new DefaultSchemaConvention("MYSCHEMA"));
        }
    }

    [Table("CUSTOMER")]
    public class CustomerEntity
    {
        [Column(Name = "CUSTOMER_ID")]
        public int Id { get; set; }

        [Column(Name="FIRST_NAME")]
        public string FirstName { get; set; }
    }

This will generate the SQL code

Code: Select all

SELECT 
"Extent1".CUSTOMER_ID AS CUSTOMER_ID, 
"Extent1".FIRST_NAME AS FIRST_NAME
FROM MYSCHEMA."Customers" "Extent1"
What I'm expecting is:

Code: Select all

SELECT 
"Extent1".CUSTOMER_ID AS CUSTOMER_ID, 
"Extent1".FIRST_NAME AS FIRST_NAME
FROM MYSCHEMA."CUSTOMER" "Extent1"
The only way I can make it work is to repeatedly specify the schema on each POCO class or on Fluent API. Like this one:

POCO:

Code: Select all

 [Table("MYSCHEMA.CUSTOMER")]
    public class CustomerEntity
    {
        [Column(Name = "CUSTOMER_ID")]
        public int Id { get; set; }

        [Column(Name="FIRST_NAME")]
        public string FirstName { get; set; }
    }
or Fluent API:

Code: Select all

protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity().ToTable("CUSTOMER", "MYSCHEMA");
            modelBuilder.Conventions.Remove();

        }