sqlserver uppercase_如何在.NET 4.5 C#Entity Framework 6中使用Oracle和SQL Server将列映射到大写?...

I'm using C#, .NET 4.5 and Entity Framework 6 in my project. It uses both Oracle and SQL Server, depending on the installation at the client.

The approach is database-first, as this database existed already by the time we decided to change the ORM from NHibernate to Entity Framework 6.

The mapping looks like this:

ToTable(schema + ".Motorista");

Property(x => x.Criacao).HasColumnName("criacao").IsOptional();

The table and column names are all in PascalCase in the mapping, which works fine with SQL Server but, in Oracle, all the names are UpperCase which causes an error:

ORA-00942: table or view does not exist

If I manually make it uppercase, then it works fine on Oracle. But I can't do that because of compatibility to SQL Server.

How can I say to Entity Framework to uppercase all the names when using Oracle?

Can I use conventions in this scenario?

解决方案

Check the providerName attribute in the named connection string to see if your connection is for SQL Server or Oracle (OR add a redundant value in the appSettings section of the configuration). Then do what @AaronLS suggested and add a helper method to case your names correctly and apply any additional formatting. The helper method should be tasked with checking the database type as mentioned above and applying or not applying casing/formatting.

Here is an example.

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

{

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值