我将 EF 6 与 ODP.Net 一起Oracle.ManagedDataAccess
用于我的 ASP.Net MVC Web 应用程序。
我有以下Model
调用Employee
(在Model\Employee.cs
文件中):
[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
,我使用Employee
和EmployeeContext
这样的:
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);
}
}
当我收到以下内部异常错误时:
Oracle.ManagedDataAccess.Client.OracleException: ORA-00904: "Extent1"."Id": 标识符无效
异常错误发生在:
Employee employee = employeeContext.Employees.Single(x => x.ID == id);
当我使用调试输出控制台进一步调试时,我注意到 EF 生成以下 SQL 查询来获取数据:
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) )
随着我做更多的研究,我了解到由于 SQL 查询列名中存在引号" "
(例如."Id"
,".EmployeeType"
等".HourlyPay"
),查询找不到匹配的列名,因为 Oracle 数据表的列名是全部用大写字母表示(即:."ID"
、、".EMPLOYEETYPE"
等".HOURLYPAY"
)。
现在,当我将类定义更改为如下内容时:
[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# 编码器,我发现类字段/属性全部大写是很不寻常的。
我的问题是,有没有办法使用大写和小写字母的组合来保留类属性,仅用于查询,生成这些属性的大写字母版本?
同样,如果这些事情很重要,我会使用 EF6.0.0.0
和 ODP.NetOracle.ManagedDataAccess
版本4.121.2.0
和Oracle.ManagedDataAccess.EntityFramework
版本。6.121.2.0