简体   繁体   English

将多个导航属性配置到同一个表时出错

[英]Error while configuring multiple Navigation Properties to same table

I am having a weird issue with multiple navigation Properties to the same table. 我有一个奇怪的问题,多个导航属性到同一个表。

My FuelBunkerType POCO class is configured like the following: 我的FuelBunkerType POCO类配置如下:

    public class FuelBunkerType : IEventReportElement
{

    public long Id { get; set; }
    public string TypeKey { get; set; }        
    public string TypeValue { get; set; }
    public decimal? Sulphur { get; set; }
    public decimal? Water { get; set; }
    public decimal? Viscosity { get; set; }
    public decimal? Density { get; set; }
    public string BdnNumber { get; set; }
    public long? IdEventReport { get; set; }
    public long? DeactivatedByIdEventReport { get; set; }

    public IList<FuelBunkerQuantity> FuelBunkerQuantitys { get; set; }
    public EventReport EventReport { get; set; }
    public EventReport DeactivatedByEventReport { get; set; }
}

In the DbContext, my class is configured like: 在DbContext中,我的类配置如下:

    modelBuilder.Entity<FuelBunkerType>().HasKey(x => x.Id);
        if (_writeversion) modelBuilder.Entity<FuelBunkerType>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        else modelBuilder.Entity<FuelBunkerType>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<FuelBunkerType>().HasRequired(x => x.EventReport).WithMany().HasForeignKey(x => x.IdEventReport);
        modelBuilder.Entity<FuelBunkerType>().HasOptional(x => x.DeactivatedByEventReport).WithMany().HasForeignKey(x => x.DeactivatedByIdEventReport);

So far, so good. 到现在为止还挺好。 It actually worked like this until friday. 直到星期五,它实际上都是这样的。 However, when I try to fetch data from the table now, the resulting Query looks like 但是,当我尝试从表中获取数据时,生成的查询看起来像

{SELECT 
1 AS [C1], 
[Extent1].[Id] AS [Id], 
[Extent1].[TypeKey] AS [TypeKey], 
[Extent1].[TypeValue] AS [TypeValue], 
[Extent1].[Sulphur] AS [Sulphur], 
[Extent1].[Water] AS [Water], 
[Extent1].[Viscosity] AS [Viscosity], 
[Extent1].[Density] AS [Density], 
[Extent1].[BdnNumber] AS [BdnNumber], 
[Extent1].[IdEventReport] AS [IdEventReport], 
[Extent1].[DeactivatedByIdEventReport] AS [DeactivatedByIdEventReport], 
[Extent1].[EventReport_Id] AS [EventReport_Id]
FROM [FuelBunkerType] AS [Extent1]}

which obviously results in a "column name is not valid" error, so my first Configuration line (Entity().HasRequired(x=>x.EventReport)...) is ignored. 这显然导致“列名无效”错误,因此我的第一个配置行(Entity()。HasRequired(x => x.EventReport)...)被忽略。 I tried out commenting out the Configuration for DeactivatedByEventReport, it resulted in the same query except for DeactivatedByEventReport_Id (like expected), it only happens with multiple Navigation Properties to the same table. 我试着注释掉了DeactivatedByEventReport的配置,它导致了除DeactivatedByEventReport_Id之外的相同查询(如预期的那样),它只发生在同一个表的多个导航属性中。 However, when I add more navigation properties to EventReport, it only mistakenly queries for EventReport_Id, so only the first one seems to be ignored when there are multiple navigation properties. 但是,当我向EventReport添加更多导航属性时,它只会错误地查询EventReport_Id,因此当存在多个导航属性时,似乎只会忽略第一个。

Any ideas why this happens, and what to do to solve this? 任何想法为什么会发生这种情况,以及如何解决这个问题? Greetings 问候

EDIT: For the other classes corresponding to this, there are the following Fluent API configs: 编辑:对于与此对应的其他类,有以下Fluent API配置:

if (_writeversion) modelBuilder.Entity<EventReport>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        else modelBuilder.Entity<EventReport>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<EventReport>().HasKey(x => x.Id);
        modelBuilder.Entity<EventReport>().Property(x => x.Id).HasColumnName("Id");
        modelBuilder.Entity<EventReport>().Ignore(x => x.MaxEventTimeLt);
        modelBuilder.Entity<EventReport>().Ignore(x => x.MaxEventTimeUtc);
        modelBuilder.Entity<EventReport>().Ignore(x => x.MinEventTimeLt);
        modelBuilder.Entity<EventReport>().Ignore(x => x.MinEventTimeUtc);
        modelBuilder.Entity<EventReport>().Ignore(x => x.EventReportElementTyps);
        modelBuilder.Entity<EventReport>().Ignore(x => x.EventsWithUpdatedSortNumber);
        modelBuilder.Entity<EventReport>().Ignore(x => x.EventTimeUtcLoaded);
        modelBuilder.Entity<EventReport>().Ignore(x => x.HasChanged);

      modelBuilder.Entity<FuelBunkerQuantity>().HasKey(x => x.Id);
        if (_writeversion) modelBuilder.Entity<FuelBunkerQuantity>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        else modelBuilder.Entity<FuelBunkerQuantity>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<FuelBunkerQuantity>().HasOptional(x => x.FuelBunkerType).WithMany().HasForeignKey(x => x.IdFuelBunkerType);
        //modelBuilder.Entity<FuelBunkerQuantity>().HasOptional(x => x.EventReport).WithMany().HasForeignKey(x => x.IdEventReport);
        modelBuilder.Entity<FuelBunkerQuantity>().Ignore(x => x.EventReport);

The Configs on EventReport shouldn't effect anything, as you can see I disabled the navigation property on FuelBunkerQuantity to try if this was causing the error, but it didn't change the Error or the resulting Query in the slightest. EventReport上的Configs不会产生任何影响,因为你可以看到我在FuelBunkerQuantity上禁用了导航属性,试试这是否导致错误,但它没有改变错误或最终的查询。

EDIT2: The class EventReport: EDIT2:类EventReport:

public class EventReport
{
    public long Id { get; set; }


    public long? SortNumber { get; set; }
    public string ReportType { get; set; }
    public DateTime? CreationTimeUtc { get; set; }


    public DateTime? EventTimeUtc { get; set; }

    public DateTime? EventTimeUtcLoaded { get; set; }
    public DateTime? EventTimeLt { get; set; }
    public long? EventDuration { get; set; }
    public DateTime? LastSendAtUtc { get; set; }
    public DateTime? LastSendAtLt { get; set; }
    public DateTime? FirstSendAtUtc { get; set; }
    public DateTime? FirstSendAtLt { get; set; }
    public string EventQualityState { get; set; }
    public bool IsDraft { get; set; }

    [IgnoreOnEqualCheck]
    public IEnumerable<FuelBunkerRob> FuelRoB { get; set; }
    [IgnoreOnEqualCheck]
    public IEnumerable<OilBunkerRob> OilRoB { get; set; }
    [IgnoreOnEqualCheck]
    public List<FuelBunkerType> ActiveFuelTypes { get; set; }
    [IgnoreOnEqualCheck]
    public DateTime? MaxEventTimeUtc { get; set; }
    [IgnoreOnEqualCheck]
    public DateTime? MaxEventTimeLt { get; set; }
    [IgnoreOnEqualCheck]
    public DateTime? MinEventTimeUtc { get; set; }
    [IgnoreOnEqualCheck]
    public DateTime? MinEventTimeLt { get; set; }


    public List<EventReport> EventsWithUpdatedSortNumber {get; set;}


    public long? ConfigurationId { get; set; }


    public bool HasChanged { get; set; }
    public List<IEventReportElement> EventReportElements { get; set; }
}

EDIT3: I just skipped the Statement which was causing the error. EDIT3:我刚刚跳过导致错误的Statement。 Turns out FuelBunkerQuantity has wrong Translation as well (see configuration in Edit): 原来FuelBunkerQuantity也有错误的翻译(请参阅编辑中的配置):

{SELECT 
1 AS [C1], 
[Extent1].[Id] AS [Id], 
[Extent1].[IdFuelBunkerType] AS [IdFuelBunkerType], 
[Extent1].[MassDelta] AS [MassDelta], 
[Extent1].[MassAbsolut] AS [MassAbsolut], 
[Extent1].[IdEventReport] AS [IdEventReport], 
[Extent1].[FuelBunkerType_Id] AS [FuelBunkerType_Id]
FROM [FuelBunkerQuantity] AS [Extent1]}

Seems like something is broken, but I have no idea what. 好像有什么事情坏了,但我不知道是什么。

As you are configuring two one-to-may relationships between EventReport and FuelBunkerType and there is only one collection on the many side EF can't match the relationships. 当您在EventReportFuelBunkerType之间配置两个一对一的关系时,在许多方面只有一个集合EF无法匹配关系。 Depending on which relationship the collection property ActiveFuelTypes belongs to you can explicitly map it like this: 根据集合属性ActiveFuelTypes所属的关系,您可以像这样显式映射它:

modelBuilder.Entity<FuelBunkerQuantity>().
             HasOptional(x => x.FuelBunkerType).
             WithMany(e => e.ActiveFuelTypes).
             HasForeignKey(x => x.IdFuelBunkerType);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM