EF 6 ODP.Net Oracle.ManagedDataAccess,如何使用非大写字母的类属性? [英] EF 6 with ODP.Net Oracle.ManagedDataAccess, How to Use Non-Capital Letter for Class Properties?

查看:646
本文介绍了EF 6 ODP.Net Oracle.ManagedDataAccess,如何使用非大写字母的类属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是EF 6 ODP.Net Oracle.ManagedDataAccess 我的ASP.Net MVC的Web应用程序。

I am using the EF 6 with ODP.Net Oracle.ManagedDataAccess for my ASP.Net MVC Web application.

我有如下的模式名为员工(在型号\\ Employee.cs 文件):

I have the following Model called Employee (in Model\Employee.cs file):

[Table("TBLEMPLOYEE")]
public class Employee {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public DateTime DateOfBirth { get; set; }
    public int EmployeeType { get; set; }
    public double? AnnualSalary { get; set; }
    public double? HourlyPay { get; set; }
    public double? HoursWorked { get; set; }
    public string City { get; set; }
}

除了 EmployeeContext.cs

public class EmployeeContext : DbContext {
    public DbSet<Employee> Employees { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.HasDefaultSchema("myschema");
    }
}

然后,在我的 EmployeeController.cs ,我用的是员工 EmployeeContext 是这样的:

Then, in my EmployeeController.cs, I use the Employee and the EmployeeContext like this:

public ActionResult Details(int id = 1) {
    try {
        EmployeeContext employeeContext = new EmployeeContext();
        employeeContext.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        Employee employee = employeeContext.Employees.Single(x => x.ID == id);
        return View(employee);
    } catch (Exception e) {
        return View(e);
    }
}

当我碰到下面的内部异常错误:

When I get the following Inner exception error:

Oracle.ManagedDataAccess.Client.OracleException:ORA-00904:Extent1,ID:无效的标识符

Oracle.ManagedDataAccess.Client.OracleException: ORA-00904: "Extent1"."Id": invalid identifier

在发生异常错误:

Employee employee = employeeContext.Employees.Single(x => x.ID == id);

当我用我的调试输出控制台进一步调试,我注意到了EF生成以下SQL查询来获取数据:

As I debug further by using my Debug output console, I noticed that the EF generates the following SQL query to fetch the data:

SELECT 
"Extent1"."Id" AS "Id", 
"Extent1"."Name" AS "Name", 
"Extent1"."Gender" AS "Gender", 
"Extent1"."DateOfBirth" AS "DateOfBirth", 
"Extent1"."EmployeeType" AS "EmployeeType", 
"Extent1"."AnnualSalary" AS "AnnualSalary", 
"Extent1"."HourlyPay" AS "HourlyPay", 
"Extent1"."HoursWorked" AS "HoursWorked", 
"Extent1"."City" AS "City"
FROM "MYSCHEMA"."TBLEMPLOYEE" "Extent1"
WHERE ("Extent1"."Id" = :p__linq__0) AND (ROWNUM <= (2) )

和我做一些更多的研究,据我所知,由于引号的present 在SQL查询中列名(如。ID。EmployeeType。HourlyPay等),查询无法找到的列名相匹配的Oracle数据表列名是全部重新$ p $大写字母psented(即:ID。EMPLOYEETYPE。HOURLYPAY等)。

And as I do some more research, I understand that due to the present of the quotation marks " " in the SQL query column names (such as ."Id", ".EmployeeType", ".HourlyPay" and so on), the query cannot find matching Column names as the Oracle Data Table column name is all represented in capital letters (that is: ."ID", ".EMPLOYEETYPE", ".HOURLYPAY" and so on).

现在,当我改变了类定义成是这样的:

Now, when I changed the class definition into something like this:

[Table("TBLEMPLOYEE")]
public class Employee {
    public int ID { get; set; }
    public string NAME { get; set; }
    public string GENDER { get; set; }
    public DateTime DATEOFBIRTH { get; set; }
    public int EMPLOYEETYPE { get; set; }
    public double? ANNUALSALARY { get; set; }
    public double? HOURLYPAY { get; set; }
    public double? HOURSWORKED { get; set; }
    public string CITY { get; set; }
}

它完美的罚款。的但我不希望出现这种情况

作为一个典型的C#codeR,我发现它的相当不寻常的的有类的字段/属性的所有大写字母。

As a typical C# coder, I found it quite unusual to have all capital letters for class fields/properties.

我的问题是,有没有什么办法留住使用大写和小写字母组合还只是用于查询类的属性,产生的这些属性的大写字母版本?

My question is, is there any way to retain the class properties using combination of capital and small letters yet, only for querying, generated capitalized letters version of those properties?

再次我用EF 6.0.0.0 和ODP.Net Oracle.ManagedDataAccess 版本 4.121.2.0 Oracle.ManagedDataAccess.EntityFramework 版本 6.121.2.0 如果这些东西此事。

Again, I use EF 6.0.0.0 and ODP.Net Oracle.ManagedDataAccess version 4.121.2.0 and Oracle.ManagedDataAccess.EntityFramework version 6.121.2.0 if those things matter.

推荐答案

您可以使用各个属性的的数据属性来指定要使用的时候对应的字段名表创建,一样一样的属性。然后,您的实际属性名可以正确的格式:

You can use the Column data attribute on each property to specify the mapped column name to use when the table is created, much the same as the Table attribute. Then, your actual property names can be correctly formatted:

[Table("TBLEMPLOYEE")]
public class Employee {
    [Column("ID")]
    public int Id { get; set; }

    [Column("NAME")]
    public string Name { get; set; }

    etc...
}

更多信息: https://msdn.microsoft.com/en -us /数据/ jj591583.aspx#TableColumn的

您还可以使用流利的API在的DbContext 来指定列名映射:

You can also use the fluent API in your DbContext to specify a column name mapping:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //Configure Column
    modelBuilder.Entity<Employee>()
                .Property(p => p.Id)
                .HasColumnName("ID");

    modelBuilder.Entity<Employee>()
                .Property(p => p.Name)
                .HasColumnName("NAME");
}

参考:<一href=\"http://www.entityframeworktutorial.net/$c$c-first/configure-property-mappings-using-fluent-api.aspx\" rel=\"nofollow\">http://www.entityframeworktutorial.net/$c$c-first/configure-property-mappings-using-fluent-api.aspx

这篇关于EF 6 ODP.Net Oracle.ManagedDataAccess,如何使用非大写字母的类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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