Code First模型在一台PC上运行,但在另一台PC上运行

时间:2012-10-26 16:11:58

标签: entity-framework-4.1 ef-code-first

背景

使用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();
        }
    }
}

在我的电脑上,这是为该实体生成的架构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,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 文件:/// C:/apps/Cultivate.TestConsole/EntityFramework.DLL

汇编:Devart.Data.Oracle.Entity Devart.Data.Oracle.Entity,Version = 7.2.96.0,Culture = neutral,PublicKeyToken = 09af7300eec23701 文件:/// C:/apps/Cultivate.TestConsole/Devart.Data.Oracle.Entity.DLL

程序集:Devart.Data.Oracle Devart.Data.Oracle,Version = 7.2.96.0,Culture = neutral,PublicKeyToken = 09af7300eec23701 文件:/// C:/apps/Cultivate.TestConsole/Devart.Data.Oracle.DLL

汇编:Devart.Data Devart.Data,Version = 5.0.555.0,Culture = neutral,PublicKeyToken = 09af7300eec23701 文件:/// C:/apps/Cultivate.TestConsole/Devart.Data.DLL

DEV PC

汇编:EntityFramework EntityFramework,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 文件:/// C:/dev/tfs/cul2/source/Cul2.Actions.Lab.TestConsole/bin/Debug/EntityFramework.DLL

汇编:Devart.Data.Oracle.Entity Devart.Data.Oracle.Entity,Version = 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,Version = 7.2.96.0,Culture = neutral,PublicKeyToken = 09af7300eec23701 文件:/// C:/Windows/assembly/GAC_MSIL/Devart.Data.Oracle/7.2.96.0__09af7300eec23701/Devart.Data.Oracle.dll

汇编:Devart.Data Devart.Data,Version = 5.0.555.0,Culture = neutral,PublicKeyToken = 09af7300eec23701 文件:/// C:/Windows/assembly/GAC_MSIL/Devart.Data/5.0.555.0__09af7300eec23701/Devart.Data.dll

0 个答案:

没有答案