如何在.NET 4.5 C#Entity Framework 6中使用Oracle和SQL Server将列映射到大写? [英] How do I map a column to uppercase in .NET 4.5 C# Entity Framework 6 using both Oracle and SQL Server?

查看:2020
本文介绍了如何在.NET 4.5 C#Entity Framework 6中使用Oracle和SQL Server将列映射到大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用C#,.NET 4.5和实体框架 6中。它使用Oracle和SQL Server,具体取决于客户端上的安装。



这种方法是数据库优先的,因为我们决定更改时已经存在这个数据库从 NHibernate 到Entity Framework 6的ORM。



映射如下所示:

  ToTable(schema +.Motorista); 
属性(x => x.Criacao).HasColumnName(criacao)。IsOptional();

映射中的表和列名都在PascalCase中,它与SQL Server兼容,在Oracle中,所有名称都是UpperCase,导致错误:


ORA-00942:表或视图不存在


如果我手动使它大写,那么它在Oracle上正常工作。但是我不能这样做,因为与SQL Server的兼容性。



当使用Oracle时,如何说Entity Framework大写所有的名称?

$

解决方案

检查命名中的providerName属性连接字符串,以查看您的连接是否用于SQL Server或Oracle(或在配置的appSettings部分中添加冗余值)。然后做什么@AaronLS建议和添加一个帮助方法,以正确的名称,并应用任何其他格式化。辅助方法应该负责检查上述数据库类型,并应用或不应用套/格式化。



这是一个示例。

  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;组; }
public string SomeMappedColumn {get;组; }
}

public class SomeMappedTypeMapper:EntityTypeConfiguration< SomeMappedType>
{
public SomeMappedTypeMapper()
{
this.HasKey(x => x.SomeMappedColumnId);
this.ToTable(SomeMappedType); //如果需要,应用与列名扩展中使用的相同的技术

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


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

这篇关于如何在.NET 4.5 C#Entity Framework 6中使用Oracle和SQL Server将列映射到大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆