我收到以下异常
System.Data.SqlClient.SqlException:为“ Join4”多次指定了“ fkidModelHardware1”列。 为“ Join7”多次指定了“ fkidModelHardware2”列。
使用SQL事件探查器,我拦截了该查询:
exec sp_executesql N'SELECT
[Limit1].[idAssetHardware] AS [idAssetHardware],
[Limit1].[UID] AS [UID],
[Limit1].[fkidAssetHardwareParent] AS [fkidAssetHardwareParent],
[Limit1].[fkidAssetHardwareParentPermanent] AS [fkidAssetHardwareParentPermanent],
[Limit1].[AssetTag] AS [AssetTag],
[Limit1].[SerialNumber] AS [SerialNumber],
[Limit1].[Hostname] AS [Hostname],
[Limit1].[Alias] AS [Alias],
[Limit1].[fkidModel] AS [fkidModel],
[Limit1].[fkidRoom] AS [fkidRoom],
[Limit1].[Nomad] AS [Nomad],
[Limit1].[ManyUsers] AS [ManyUsers],
[Limit1].[fkidDepartment] AS [fkidDepartment],
[Limit1].[fkidUserAD] AS [fkidUserAD],
[Limit1].[fkidStatus] AS [fkidStatus],
[Limit1].[EndOfLeaseDate] AS [EndOfLeaseDate],
[Limit1].[fkidOwner] AS [fkidOwner],
[Limit1].[Suspect] AS [Suspect],
[Limit1].[LastChecked] AS [LastChecked],
[Limit1].[Remark] AS [Remark],
[Limit1].[LastModifiedBy] AS [LastModifiedBy],
[Limit1].[LastModifiedOn] AS [LastModifiedOn],
[Limit1].[fkidAssetService] AS [fkidAssetService],
[Limit1].[SCCM_ResourceID] AS [SCCM_ResourceID],
[Limit1].[fkidOs] AS [fkidOs],
[Limit1].[fkidProfile] AS [fkidProfile],
[Limit1].[InvDate] AS [InvDate],
[Limit1].[fkidNetwork] AS [fkidNetwork],
[Limit1].[fkidAssetServiceWarranty] AS [fkidAssetServiceWarranty],
[Limit1].[MacAdress] AS [MacAdress],
[Limit1].[WarrantyExtensionYears] AS [WarrantyExtensionYears],
[Limit1].[fkidLeasingContract] AS [fkidLeasingContract],
[Limit1].[SAP] AS [SAP],
[Limit1].[Contact] AS [Contact],
[Limit1].[idModel] AS [idModel],
[Limit1].[ModelName] AS [ModelName],
[Limit1].[PartNumber] AS [PartNumber],
[Limit1].[fkidBrand] AS [fkidBrand],
[Limit1].[fkidCategory] AS [fkidCategory],
[Limit1].[fkidFamily] AS [fkidFamily],
[Limit1].[fkidType] AS [fkidType],
[Limit1].[Picture] AS [Picture],
[Limit1].[RequestSerialNumber] AS [RequestSerialNumber],
[Limit1].[AssetSuffix] AS [AssetSuffix],
[Limit1].[BufferStock] AS [BufferStock],
[Limit1].[SlavePermanent] AS [SlavePermanent],
[Limit1].[ImportantForInv] AS [ImportantForInv],
[Limit1].[ExcludeFromDashboard] AS [ExcludeFromDashboard],
[Limit1].[DefaultWarrantyLengthYears] AS [DefaultWarrantyLengthYears],
[Limit1].[ToFollowUp] AS [ToFollowUp],
[Limit1].[ForceShowProfile] AS [ForceShowProfile],
[Limit1].[ForceShowSvcDetails] AS [ForceShowSvcDetails],
[Limit1].[idProductCode] AS [idProductCode],
[Limit1].[ProductDescription] AS [ProductDescription],
[Limit1].[Category] AS [Category],
[Limit1].[NeedDelivery] AS [NeedDelivery],
[Limit1].[fkidDealer] AS [fkidDealer],
[Limit1].[fkidModelHardware] AS [fkidModelHardware],
[Limit1].[fkidSoftwareCatalog] AS [fkidSoftwareCatalog],
[Limit1].[fkidServiceCatalog] AS [fkidServiceCatalog],
[Limit1].[ProductPicture] AS [ProductPicture],
[Limit1].[SlaDelivery] AS [SlaDelivery],
[Limit1].[ReplacedBy] AS [ReplacedBy],
[Limit1].[fkidDefaultOsRollIn] AS [fkidDefaultOsRollIn],
[Limit1].[fkidModelHardware1] AS [fkidModelHardware1]
FROM ( SELECT TOP (1)
[Extent1].[idAssetHardware] AS [idAssetHardware],
[Extent1].[UID] AS [UID],
[Extent1].[fkidAssetHardwareParent] AS [fkidAssetHardwareParent],
[Extent1].[fkidAssetHardwareParentPermanent] AS [fkidAssetHardwareParentPermanent],
[Extent1].[AssetTag] AS [AssetTag],
[Extent1].[SerialNumber] AS [SerialNumber],
[Extent1].[Hostname] AS [Hostname],
[Extent1].[Alias] AS [Alias],
[Extent1].[fkidModel] AS [fkidModel],
[Extent1].[fkidRoom] AS [fkidRoom],
[Extent1].[Nomad] AS [Nomad],
[Extent1].[ManyUsers] AS [ManyUsers],
[Extent1].[fkidDepartment] AS [fkidDepartment],
[Extent1].[fkidUserAD] AS [fkidUserAD],
[Extent1].[fkidStatus] AS [fkidStatus],
[Extent1].[EndOfLeaseDate] AS [EndOfLeaseDate],
[Extent1].[fkidOwner] AS [fkidOwner],
[Extent1].[Suspect] AS [Suspect],
[Extent1].[LastChecked] AS [LastChecked],
[Extent1].[Remark] AS [Remark],
[Extent1].[LastModifiedBy] AS [LastModifiedBy],
[Extent1].[LastModifiedOn] AS [LastModifiedOn],
[Extent1].[fkidAssetService] AS [fkidAssetService],
[Extent1].[SCCM_ResourceID] AS [SCCM_ResourceID],
[Extent1].[fkidOs] AS [fkidOs],
[Extent1].[fkidProfile] AS [fkidProfile],
[Extent1].[InvDate] AS [InvDate],
[Extent1].[fkidNetwork] AS [fkidNetwork],
[Extent1].[fkidAssetServiceWarranty] AS [fkidAssetServiceWarranty],
[Extent1].[MacAdress] AS [MacAdress],
[Extent1].[WarrantyExtensionYears] AS [WarrantyExtensionYears],
[Extent1].[fkidLeasingContract] AS [fkidLeasingContract],
[Extent1].[SAP] AS [SAP],
[Extent1].[Contact] AS [Contact],
[Join1].[idModel] AS [idModel],
[Join1].[ModelName] AS [ModelName],
[Join1].[PartNumber] AS [PartNumber],
[Join1].[fkidBrand] AS [fkidBrand],
[Join1].[fkidCategory] AS [fkidCategory],
[Join1].[fkidFamily] AS [fkidFamily],
[Join1].[fkidType] AS [fkidType],
[Join1].[Picture] AS [Picture],
[Join1].[RequestSerialNumber] AS [RequestSerialNumber],
[Join1].[AssetSuffix] AS [AssetSuffix],
[Join1].[BufferStock] AS [BufferStock],
[Join1].[SlavePermanent] AS [SlavePermanent],
[Join1].[ImportantForInv] AS [ImportantForInv],
[Join1].[ExcludeFromDashboard] AS [ExcludeFromDashboard],
[Join1].[DefaultWarrantyLengthYears] AS [DefaultWarrantyLengthYears],
[Join1].[ToFollowUp] AS [ToFollowUp],
[Join1].[ForceShowProfile] AS [ForceShowProfile],
[Join1].[ForceShowSvcDetails] AS [ForceShowSvcDetails],
[Join4].[idProductCode1] AS [idProductCode],
[Join4].[ProductDescription1] AS [ProductDescription],
[Join4].[Category1] AS [Category],
[Join4].[NeedDelivery1] AS [NeedDelivery],
[Join4].[fkidDealer1] AS [fkidDealer],
[Join4].[fkidModelHardware1] AS [fkidModelHardware],
[Join4].[fkidSoftwareCatalog1] AS [fkidSoftwareCatalog],
[Join4].[fkidServiceCatalog1] AS [fkidServiceCatalog],
[Join4].[ProductPicture1] AS [ProductPicture],
[Join4].[SlaDelivery1] AS [SlaDelivery],
[Join4].[ReplacedBy1] AS [ReplacedBy],
[Join4].[fkidDefaultOsRollIn1] AS [fkidDefaultOsRollIn],
[Join7].[fkidModelHardware2] AS [fkidModelHardware1]
FROM [dbo].[t_AssetsHardwares] AS [Extent1]
INNER JOIN (SELECT [Extent2].[idModel] AS [idModel], [Extent2].[ModelName] AS [ModelName], [Extent2].[PartNumber] AS [PartNumber], [Extent2].[fkidBrand] AS [fkidBrand], [Extent2].[fkidCategory] AS [fkidCategory], [Extent2].[fkidFamily] AS [fkidFamily], [Extent2].[fkidType] AS [fkidType], [Extent2].[Picture] AS [Picture], [Extent2].[RequestSerialNumber] AS [RequestSerialNumber], [Extent2].[AssetSuffix] AS [AssetSuffix], [Extent2].[BufferStock] AS [BufferStock], [Extent2].[SlavePermanent] AS [SlavePermanent], [Extent2].[ImportantForInv] AS [ImportantForInv], [Extent2].[ExcludeFromDashboard] AS [ExcludeFromDashboard], [Extent2].[DefaultWarrantyLengthYears] AS [DefaultWarrantyLengthYears], [Extent2].[ToFollowUp] AS [ToFollowUp], [Extent2].[ForceShowProfile] AS [ForceShowProfile], [Extent2].[ForceShowSvcDetails] AS [ForceShowSvcDetails]
FROM [dbo].[t_Models] AS [Extent2]
LEFT OUTER JOIN [dbo].[t_MIT_Products] AS [Extent3] ON ([Extent2].[idModel] = [Extent3].[FkidModelHardware]) AND ([Extent3].[FkidModelHardware] IS NOT NULL) ) AS [Join1] ON [Extent1].[fkidModel] = [Join1].[idModel]
LEFT OUTER JOIN (SELECT [Extent4].[idProductCode] AS [idProductCode1], [Extent4].[ProductDescription] AS [ProductDescription1], [Extent4].[Category] AS [Category1], [Extent4].[NeedDelivery] AS [NeedDelivery1], [Extent4].[fkidDealer] AS [fkidDealer1], [Extent4].[fkidModelHardware] AS [fkidModelHardware1], [Extent4].[fkidSoftwareCatalog] AS [fkidSoftwareCatalog1], [Extent4].[fkidServiceCatalog] AS [fkidServiceCatalog1], [Extent4].[ProductPicture] AS [ProductPicture1], [Extent4].[SlaDelivery] AS [SlaDelivery1], [Extent4].[ReplacedBy] AS [ReplacedBy1], [Extent4].[fkidDefaultOsRollIn] AS [fkidDefaultOsRollIn1], [Extent4].[FkidModelHardware] AS [fkidModelHardware1], [Join3].[idModel]
FROM [dbo].[t_MIT_Products] AS [Extent4]
INNER JOIN (SELECT [Extent5].[idModel] AS [idModel]
FROM [dbo].[t_Models] AS [Extent5]
LEFT OUTER JOIN [dbo].[t_MIT_Products] AS [Extent6] ON ([Extent5].[idModel] = [Extent6].[FkidModelHardware]) AND ([Extent6].[FkidModelHardware] IS NOT NULL) ) AS [Join3] ON [Extent4].[FkidModelHardware] = [Join3].[idModel] ) AS [Join4] ON [Extent1].[fkidModel] = [Join4].[idModel]
LEFT OUTER JOIN (SELECT [Extent7].[fkidModelHardware] AS [fkidModelHardware2], [Extent7].[FkidModelHardware] AS [fkidModelHardware2], [Join6].[idModel]
FROM [dbo].[t_MIT_Products] AS [Extent7]
INNER JOIN (SELECT [Extent8].[idModel] AS [idModel]
FROM [dbo].[t_Models] AS [Extent8]
LEFT OUTER JOIN [dbo].[t_MIT_Products] AS [Extent9] ON ([Extent8].[idModel] = [Extent9].[FkidModelHardware]) AND ([Extent9].[FkidModelHardware] IS NOT NULL) ) AS [Join6] ON [Extent7].[FkidModelHardware] = [Join6].[idModel] ) AS [Join7] ON [Extent1].[fkidModel] = [Join7].[idModel]
WHERE [Extent1].[idAssetHardware] = @p__linq__0
) AS [Limit1]',N'@p__linq__0 int',@p__linq__0=134955
以下是产生错误的LINQ查询:
_unitOfWork.AssetHardwares.Include(ah => ah.Model)
.Include(ah => ah.Model.MITProduct)
.FirstOrDefault(x => x.Id == assetHardwareId);
我的映射类:
using ITAM.Domain.MIT;
using System.Data.Entity.ModelConfiguration;
namespace ITAM.DAL.Mapping
{
public class MITProductMap : EntityTypeConfiguration<MITProduct>
{
public MITProductMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.Id)
.IsRequired()
.HasMaxLength(255);
this.Property(t => t.ProductDescription)
.IsRequired()
.HasMaxLength(255);
this.Property(t => t.ReplacedBy)
.HasMaxLength(25);
// Table & Column Mappings
this.ToTable("t_MIT_Products");
this.Property(t => t.Id).HasColumnName("idProductCode");
this.Property(t => t.ProductDescription).HasColumnName("ProductDescription");
this.Property(t => t.Category).HasColumnName("Category");
this.Property(t => t.NeedDelivery).HasColumnName("NeedDelivery");
this.Property(t => t.FkidDealer).HasColumnName("fkidDealer");
this.Property(t => t.FkidModelHardware).HasColumnName("fkidModelHardware");
this.Property(t => t.FkidSoftwareCatalog).HasColumnName("fkidSoftwareCatalog");
this.Property(t => t.FkidServiceCatalog).HasColumnName("fkidServiceCatalog");
this.Property(t => t.ProductPicture).HasColumnName("ProductPicture");
this.Property(t => t.SlaDelivery).HasColumnName("SlaDelivery");
this.Property(t => t.ReplacedBy).HasColumnName("ReplacedBy");
this.Property(t => t.FkidDefaultOsRollIn).HasColumnName("fkidDefaultOsRollIn");
// Relationships
this.HasOptional(t => t.SoftwareCatalog)
.WithMany(t => t.MITProducts)
.HasForeignKey(d => d.FkidSoftwareCatalog);
//If you want to consider the relationship between t_Models and t_MIT_Products as a one to many. (standard created by tool)
//this.HasOptional(t => t.Model)
// .WithMany()
// .HasForeignKey(d => d.FkidModelHardware);
//I've succeeded to consider the relationship between t_Models and t_MIT_Products as a one to one with the following syntax.
//https://www.codeproject.com/Articles/806344/One-to-zero-one-relation-in-entity-framework-code
this.HasOptional(t => t.Model)
.WithOptionalDependent(t => t.MITProduct).Map(x => x.MapKey("FkidModelHardware"));
}
}
}
使用显式加载时,我没有问题:
//Solution: use explicit loading for loading the MITProduct
//http://www.entityframeworktutorial.net/entityframework4.3/explicit-loading-with-dbcontext.aspx
var assetHardware = _unitOfWork.AssetHardwares
.Include(ah => ah.Model)
.FirstOrDefault(x => x.Id == assetHardwareId);
_unitOfWork.Entry(assetHardware.Model)
.Reference(m => m.MITProduct).Load();
这是LINQ查询生成中的错误吗?