背景
使用 ef4.1 的代码优先模型。数据库是 Oracle,所以我们也在使用 Devart dotConnect。
问题
将应用程序部署到服务器后,看到 sql 语句和无效列名的错误。在我的开发 PC 上一切正常。
创建了一个小型测试控制台应用程序并复制了该行为。在我的电脑上,不是调试而是运行应用程序,它工作正常并正确选择数据。在服务器上,select 语句有两个额外的外键列名。
实体
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.ServiceModel.DomainServices.Server;
using log4net;
namespace Cul2.Model
{
[Table("LAB_REQUISITION")]
public class LabRequisition : EntityBase
{
public LabRequisition()
{
Attributes = new Collection<LabRequisitionAttribute>();
Samples = new Collection<LabSample>();
}
[Key]
[Column("REQUISITION_ID")]
public long? RequisitionId { get; set; }
[Column("MATERIAL_ID")]
public long? MaterialId { get; set; }
[Include]
[Association("Material", "MaterialId", "MaterialId", IsForeignKey = true)]
public virtual LabMaterial Material { get; set; }
[Column("LAB_NUMBER")]
public int? LabNumber { get; set; }
[Column("COLLECTOR_ID")]
public long? CollectorId { get; set; }
[Include]
[Association("Collector", "CollectorId", "UserId")]
[ForeignKey("CollectorId")]
public virtual ComUser Collector { get; set; }
[Column("RECEIVER_ID")]
public long? ReceiverId { get; set; }
[Include]
[Association("Receiver", "ReceiverId", "UserId")]
[ForeignKey("ReceiverId")]
public virtual ComUser Receiver { get; set; }
[Column("COLLECTION_DATE")]
public DateTime? CollectionDate { get; set; }
[Column("STATUS_ID")]
public long? StatusId { get; set; }
[Include]
[Association("Status", "StatusId", "StatusId", IsForeignKey = true)]
public virtual LabStatus Status { get; set; }
[Column("IS_ACTIVE")]
public bool IsActive { get; set; }
[Column("TEMPLATE_ID")]
public long? TemplateId { get; set; }
[Include]
[Association("Template", "TemplateId", "TemplateId", IsForeignKey = true)]
public virtual LabTemplate Template { get; set; }
[Column("REASON_ID")]
public long? ReasonId { get; set; }
[Include]
[Association("Reason", "ReasonId", "ReasonId", IsForeignKey = true)]
public virtual LabReason Reason { get; set; }
[Column("RECEIVED_DATE")]
public DateTime? ReceivedDate { get; set; }
[Include]
[Association("Samples", "RequisitionId", "RequisitionId")]
[ForeignKey("RequisitionId")]
public ICollection<LabSample> Samples { get; set; }
[Include]
[Association("Attributes", "RequisitionId", "RequisitionId")]
public ICollection<LabRequisitionAttribute> Attributes { get; set; }
public override string ToString()
{
return LabNumber == null ? "New" : LabNumber.ToString();
}
}
}
在我的 PC 上,这是为该实体生成的模式 sql:
-- Table LAB_REQUISITION
CREATE TABLE LAB_REQUISITION (
REQUISITION_ID NUMBER(18) NOT NULL,
MATERIAL_ID NUMBER(18) NULL,
LAB_NUMBER NUMBER(10) NULL,
COLLECTOR_ID NUMBER(18) NULL,
RECEIVER_ID NUMBER(18) NULL,
COLLECTION_DATE TIMESTAMP(7) NULL,
STATUS_ID NUMBER(18) NULL,
IS_ACTIVE NUMBER(1) NOT NULL,
TEMPLATE_ID NUMBER(18) NULL,
REASON_ID NUMBER(18) NULL,
RECEIVED_DATE TIMESTAMP(7) NULL,
ROW_VERSION NUMBER(18,2) NULL,
CREATED TIMESTAMP(7) NULL,
CREATED_BY_ID NUMBER(18) NULL,
LAST_UPDATED TIMESTAMP(7) NULL,
LAST_UPDATED_BY_ID NUMBER(18) NULL,
PRIMARY KEY (REQUISITION_ID)
)
/
在服务器上,这是生成的架构:
-- Table LAB_REQUISITION
CREATE TABLE LAB_REQUISITION (
REQUISITION_ID NUMBER(18) NOT NULL,
MATERIAL_ID NUMBER(18) NULL,
LAB_NUMBER NUMBER(10) NULL,
COLLECTOR_ID NUMBER(18) NULL,
RECEIVER_ID NUMBER(18) NULL,
COLLECTION_DATE TIMESTAMP(7) NULL,
STATUS_ID NUMBER(18) NULL,
IS_ACTIVE NUMBER(1) NOT NULL,
TEMPLATE_ID NUMBER(18) NULL,
REASON_ID NUMBER(18) NULL,
RECEIVED_DATE TIMESTAMP(7) NULL,
ROW_VERSION NUMBER(18,2) NULL,
CREATED TIMESTAMP(7) NULL,
CREATED_BY_ID NUMBER(18) NULL,
LAST_UPDATED TIMESTAMP(7) NULL,
LAST_UPDATED_BY_ID NUMBER(18) NULL,
"Collector_UserId" NUMBER(18) NULL,
"Receiver_UserId" NUMBER(18) NULL,
PRIMARY KEY (REQUISITION_ID)
)
/
Collector_UserId和Receiver_UserId列不正确,它们导致 SQL 选择错误。
我无法弄清楚为什么该模型会在我的 Dev PC 上生成正确的 SQL,但在服务器上它会生成虚假的额外列。
该应用程序在 Dev PC 和服务器上完全相同,除了在服务器上我必须将 DbProviderFactories 部分添加到 app.config 中——在我的 PC 上,我猜因为安装了 Devart,它已经注册。否则完全一样。
在我的 Dev PC 上,Devart 库是从 GAC 加载的,而在服务器上,它们是从 bin 文件夹加载的。据我所知,版本是相同的。
更新 1 - 版本:
这些是 EF 和 devart 程序集的版本和路径。
服务器
程序集:EntityFramework EntityFramework,版本=4.1.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 file:///C:/apps/Cultivate.TestConsole/EntityFramework.DLL
程序集:Devart.Data.Oracle.Entity Devart.Data.Oracle.Entity,版本=7.2.96.0,Culture=neutral,PublicKeyToken=09af7300eec23701 file:///C:/apps/Cultivate.TestConsole/Devart.Data.Oracle。实体.DLL
程序集: Devart.Data.Oracle Devart.Data.Oracle,版本=7.2.96.0,Culture=neutral,PublicKeyToken=09af7300eec23701 file:///C:/apps/Cultivate.TestConsole/Devart.Data.Oracle.DLL
程序集: Devart.Data Devart.Data,版本=5.0.555.0,Culture=neutral,PublicKeyToken=09af7300eec23701 file:///C:/apps/Cultivate.TestConsole/Devart.Data.DLL
开发电脑
程序集:EntityFramework EntityFramework,版本=4.1.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 file:///C:/dev/tfs/cul2/source/Cul2.Actions.Lab.TestConsole/bin/Debug/EntityFramework.DLL
程序集:Devart.Data.Oracle.Entity Devart.Data.Oracle.Entity,版本=7.2.96.0,Culture=neutral,PublicKeyToken=09af7300eec23701 文件:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Devart。 Data.Oracle.Entity/v4.0_7.2.96.0__09af7300eec23701/Devart.Data.Oracle.Entity.dll
程序集:Devart.Data.Oracle Devart.Data.Oracle,版本=7.2.96.0,Culture=neutral,PublicKeyToken=09af7300eec23701 file:///C:/Windows/assembly/GAC_MSIL/Devart.Data.Oracle/7.2.96.0__09af7300eec23701 /Devart.Data.Oracle.dll
程序集: Devart.Data Devart.Data,版本=5.0.555.0,Culture=neutral,PublicKeyToken=09af7300eec23701 file:///C:/Windows/assembly/GAC_MSIL/Devart.Data/5.0.555.0__09af7300eec23701/Devart.Data.dll