How do I map a column to uppercase in .NET 4.5 C# Entity Framework 6 using both Oracle and SQL Server?

c#-4.0 entity-framework entity-framework-6 oracle sql-server

Question

In my project, I'm using C#,.NET 4.5, and Enterprise Framework 6. Depending on the client's installation, it uses both Oracle and SQL Server.

Since this database already existed when we decided to switch from NHibernate to Entity Framework 6, the strategy is database-first.

The mapping appears as follows:

ToTable(schema + ".Motorista");
Property(x => x.Criacao).HasColumnName("criacao").IsOptional();

The mapping's table and column names are all in PascalCase, which works perfectly with SQL Server, but all the names in Oracle are in UpperCase, which results in an error.

ORA-00942: table or view does not exist

It works on Oracle without a problem if I manually make it uppercase. However, due to compatibility with SQL Server, I am unable to accomplish it.

How do I instruct Oracle's Entity Framework to capitalize every name?

In this situation, may I utilize conventions?

1
3
2/17/2016 11:11:33 PM

Accepted Answer

To determine if your connection is for Oracle or SQL Server, look at the providerName property in the named connection string (OR add a redundant value in the appSettings section of the configuration). Do as @AaronLS advised and add a helper method to correctly case your names and apply any additional formatting after that. Checking the database type, as described above, and applying or not applying casing/formatting should be the responsibility of the helper method.

Here's an illustration.

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new SomeMappedTypeMapper());
        base.OnModelCreating(modelBuilder);
    }
}

public class SomeMappedType
{
    public int SomeMappedColumnId { get; set; }
    public string SomeMappedColumn { get; set; }
}

public class SomeMappedTypeMapper : EntityTypeConfiguration<SomeMappedType>
{
    public SomeMappedTypeMapper()
    {
        this.HasKey(x => x.SomeMappedColumnId);
        this.ToTable("SomeMappedType"); // If needed, apply the same technique as used in the column name extension

        this.Property(x => x.SomeMappedColumnId).HasColumnNameV2("SomeMappedColumnId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        this.Property(x => x.SomeMappedColumn).HasColumnNameV2("SomeMappedColumn");
    }
}

public static class TypeHelper
{
    private static bool isOracle;
    static TypeHelper()
    {
        isOracle = System.Configuration.ConfigurationManager.ConnectionStrings["yourDbConnectionName"].ProviderName.IndexOf("oracle", StringComparison.OrdinalIgnoreCase) >= 0;
    }
    public static PrimitivePropertyConfiguration HasColumnNameV2(this PrimitivePropertyConfiguration property, string columnName)
    {
        if (isOracle)
            return property.HasColumnName(columnName.ToUpper());
        return property.HasColumnName(columnName);
    }
}
1
2/17/2016 11:08:37 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow