c# - Entity Framework 不包括选择语句中的列

标签 c# sql entity-framework

我刚刚在 Entity Framework 映射的表中添加了一列。 Entity Framework 将列添加到模型中,但不在其选择查询中包含该列。这是 EF 生成的类:

public partial class mj_bagasse_gc_sampledetail
{
    public long ID { get; set; }
    public Nullable<double> FructoseArea { get; set; }
    public Nullable<double> SucroseArea { get; set; }
    public Nullable<double> GlucoseArea { get; set; }
    public Nullable<double> TrehaloseArea { get; set; }
    public Nullable<double> SampleWeight { get; set; }
    public Nullable<double> TrehaloseWeight { get; set; }
    public string TriplicateIndex { get; set; }
    public Nullable<long> Sample_ID { get; set; }
    public Nullable<double> FructoseConc { get; set; }
    public Nullable<double> SucroseConc { get; set; }
    public Nullable<double> GlucoseConc { get; set; }
    public Nullable<int> GCIndex { get; set; }
    public string BottleNo { get; set; }
    public Nullable<bool> SelectedSampleDetail { get; set; }
    public Nullable<int> SampleDetailRun { get; set; }

    public virtual global_global_sample global_global_sample { get; set; }
}

这是 EDMX 文件中映射的 XML:

<EntityType Name="mj-bagasse_gc_sampledetail">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Name="ID" Type="bigint" Nullable="false" StoreGeneratedPattern="Identity" />
      <Property Name="FructoseArea" Type="float" />
      <Property Name="SucroseArea" Type="float" />
      <Property Name="GlucoseArea" Type="float" />
      <Property Name="TrehaloseArea" Type="float" />
      <Property Name="SampleWeight" Type="float" />
      <Property Name="TrehaloseWeight" Type="float" />
      <Property Name="TriplicateIndex" Type="varchar" MaxLength="2" />
      <Property Name="Sample_ID" Type="bigint" />
      <Property Name="FructoseConc" Type="float" />
      <Property Name="SucroseConc" Type="float" />
      <Property Name="GlucoseConc" Type="float" />
      <Property Name="GCIndex" Type="int" />
      <Property Name="BottleNo" Type="varchar" MaxLength="20" />
      <Property Name="SelectedSampleDetail" Type="bit" />
      <Property Name="SampleDetailRun" Type="int" />
    </EntityType>

您会清楚地注意到,在这两种情况下,“SampleDetailRun”列均已正确定义。但是,当我尝试使用 LINQ 语句从表中获取一行时:

(from sd in Entities.mj_bagasse_gc_sampledetail 
                                  where (sd.Sample_ID == Sample.ID) && 
                                  (sd.TriplicateIndex == s) select sd).FirstOrDefault();

这条SQL语句执行:

exec sp_executesql N'SELECT TOP (1) 
[Extent1].[ID] AS [ID], 
[Extent1].[FructoseArea] AS [FructoseArea], 
[Extent1].[SucroseArea] AS [SucroseArea], 
[Extent1].[GlucoseArea] AS [GlucoseArea], 
[Extent1].[TrehaloseArea] AS [TrehaloseArea], 
[Extent1].[SampleWeight] AS [SampleWeight], 
[Extent1].[TrehaloseWeight] AS [TrehaloseWeight], 
[Extent1].[TriplicateIndex] AS [TriplicateIndex], 
[Extent1].[Sample_ID] AS [Sample_ID], 
[Extent1].[FructoseConc] AS [FructoseConc], 
[Extent1].[SucroseConc] AS [SucroseConc], 
[Extent1].[GlucoseConc] AS [GlucoseConc], 
[Extent1].[GCIndex] AS [GCIndex], 
[Extent1].[BottleNo] AS [BottleNo]
FROM [dbo].[mj-bagasse_gc_sampledetail] AS [Extent1]
WHERE ([Extent1].[Sample_ID] = @p__linq__0) AND ([Extent1].[TriplicateIndex] = @p__linq__1)',N'@p__linq__0 bigint,@p__linq__1 varchar(8000)',@p__linq__0=127,@p__linq__1='A'
go

select 语句中缺少 SampleDetailRun 列,我不知道如何让它包含它。我试过删除映射表并重新添加它。我试过从数据库中的表中删除该列并再次重新添加它(即使使用不同的名称),但 EF 无法在选择中包含该列。

我正在使用 EF5

有什么想法吗?谢谢

最佳答案

叹息。使用 EF 数据上下文的项目的 EF 元数据文件(*.msl、*.csdl 和 *.ssdl)位于与重建 EF 项目时更新的文件夹不同的文件夹中。当我将它们复制到使用这些文件的项目时,它在 select 语句中包含了这些列。

关于c# - Entity Framework 不包括选择语句中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982011/

相关文章:

c# - FluentValidation 和服务器+客户端远程验证器

mysql - 表很大时求每组的最大记录如何优化sql?

带有 SQL 通配符和 LIKE 的 Python 字符串格式

mysql - (MySQL) 在更新该列之前触发列将所有值设置为零?

sql - 如何让 Entity Framework 使用存档标志?

c# - F# List.map 在 C# 中等效?

c# - 如果没有空格分隔符,为什么 XmlReader 会跳过所有其他元素?

c# - Visual Studio 2008 调试器在 C# 解决方案中错误地将 C++ 显示为语言

c# - Entity Framework 插入未反射(reflect)在数据库中

c# - 如何使用 BindingSource 在 DataGridView 的单列中绑定(bind)导航属性(二级模型的两个属性)?