实体框架进行包含不存在列的查询,然后抱怨它不存在

问题描述 投票:0回答:1

我有一个基于 .net8.0 构建的应用程序。我通过搭建现有数据库表在实体框架中创建了下表:

 public partial class Shipment1850 : INotifyPropertyChanged
 {
     public event PropertyChangedEventHandler PropertyChanged;
     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
     {
         Dirty = true;
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }

     [NotMapped]
     [JsonIgnore]
     public bool Dirty { get; set; } = false;

     private Guid _Id;
     public Guid Id
     {
         get
         {
             return _Id;
         }
         set
         {
             _Id = value;
             NotifyPropertyChanged();
         }
     }
     private int? _Idno;
     public int? Idno
     {
         get
         {
             return _Idno;
         }
         set
         {
             _Idno = value;
             NotifyPropertyChanged();
         }
     }
     private short? _Idsub;
     public short? Idsub
     {
         get
         {
             return _Idsub;
         }
         set
         {
             _Idsub = value;
             NotifyPropertyChanged();
         }
     }
     private int? _Unpacking;
     public int? Unpacking
     {
         get
         {
             return _Unpacking;
         }
         set
         {
             _Unpacking = value;
             NotifyPropertyChanged();
         }
     }
     private byte[] _CustSig;
     public byte[] CustSig
     {
         get
         {
             return _CustSig;
         }
         set
         {
             _CustSig = value;
             NotifyPropertyChanged();
         }
     }
     private DateTime? _CustSigDate;
     public DateTime? CustSigDate
     {
         get
         {
             return _CustSigDate;
         }
         set
         {
             _CustSigDate = value;
             NotifyPropertyChanged();
         }
     }
     private byte[] _Tspsig;
     public byte[] Tspsig
     {
         get
         {
             return _Tspsig;
         }
         set
         {
             _Tspsig = value;
             NotifyPropertyChanged();
         }
     }
     private DateTime? _TspsigDate;
     public DateTime? TspsigDate
     {
         get
         {
             return _TspsigDate;
         }
         set
         {
             _TspsigDate = value;
             NotifyPropertyChanged();
         }
     }
     private DateTime? _Modified;
     public DateTime? Modified
     {
         get
         {
             return _Modified;
         }
         set
         {
             _Modified = value;
             NotifyPropertyChanged();
         }
     }
     private string _ModifiedBy;
     public string ModifiedBy
     {
         get
         {
             return _ModifiedBy;
         }
         set
         {
             _ModifiedBy = value;
             NotifyPropertyChanged();
         }
     }
     private bool _Complete;
     public bool Complete
     {
         get
         {
             return _Complete;
         }
         set
         {
             _Complete = value;
             NotifyPropertyChanged();
         }
     }
     private string _UserName;
     public string UserName
     {
         get
         {
             return _UserName;
         }
         set
         {
             _UserName = value;
             NotifyPropertyChanged();
         }
     }
 }

public partial class Shipment1850Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        Dirty = true;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    [NotMapped]
    [JsonIgnore]
    public bool Dirty { get; set; } = false;

    private Guid _Id;
    public Guid Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
            NotifyPropertyChanged();
        }
    }
    private Guid? _S1850id;
    public Guid? S1850id
    {
        get
        {
            return _S1850id;
        }
        set
        {
            _S1850id = value;
            NotifyPropertyChanged();
        }
    }
    private int? _InvNo;
    public int? InvNo
    {
        get
        {
            return _InvNo;
        }
        set
        {
            _InvNo = value;
            NotifyPropertyChanged();
        }
    }
    private string _Item;
    public string Item
    {
        get
        {
            return _Item;
        }
        set
        {
            _Item = value;
            NotifyPropertyChanged();
        }
    }
    private string _Description;
    public string Description
    {
        get
        {
            return _Description;
        }
        set
        {
            _Description = value;
            NotifyPropertyChanged();
        }
    }
    private DateTime? _Modified;
    public DateTime? Modified
    {
        get
        {
            return _Modified;
        }
        set
        {
            _Modified = value;
            NotifyPropertyChanged();
        }
    }
    private string _ModifiedBy;
    public string ModifiedBy
    {
        get
        {
            return _ModifiedBy;
        }
        set
        {
            _ModifiedBy = value;
            NotifyPropertyChanged();
        }
    }
}

尽管这些表在技术上是相关的,但数据库中没有外键约束。我创建了一个扩展类来保存父级中的项目:

public partial class Shipment1850
{
    public List<Shipment1850Item> Items { get; set; }

}

当我提出以下查询时:

   var id = Guid.Parse("5c059022-5b08-472e-b7fe-7ae9e4293d55");

   var s = await _context.Shipment1850Items
                   .Where(x => x.S1850id == id).ToListAsync();

我收到以下错误:

Microsoft.EntityFrameworkCore.Database.Command: Error: Failed executing DbCommand (13ms) [Parameters=[@__id_0='?' (DbType = Guid)], CommandType='Text', CommandTimeout='30']
SELECT [s].[ID], [s].[Description], [s].[InvNo], [s].[Item], [s].[Modified], [s].[ModifiedBy], [s].[S1850ID], [s].[Shipment1850Id]
FROM [Shipment1850Item] AS [s]
WHERE [s].[S1850ID] = @__id_0
'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.1\System.Reflection.Metadata.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'MPS_Tablet_Data.Entities.DispatchContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'Shipment1850Id'.

我的代码或数据库中没有任何名为 Shipment1850Id 的列。出于某种原因,它决定创建这个领域。如果我注释掉:

   //public List<Shipment1850Item> Items { get; set; }

在扩展类中,错误消失了。我在类似的表中执行此操作,并且 EF 不会自行创建不存在的外键。有谁知道如何让它停止这样做吗?

c# entity-framework entity-framework-core .net-8.0
1个回答
0
投票

答案其实很简单。格特·阿诺德的想法是正确的。在我的扩展类中,我只需使用外键属性来装饰 Items 属性的声明。像这样:

public partial class Shipment1850
{
    [ForeignKey("S1850id")]
    public ObservableCollection<Shipment1850Item> Items { get; set; }

}
© www.soinside.com 2019 - 2024. All rights reserved.