首页 > 解决方案 > 从现有表中部分加载实体

问题描述

更新:

抱歉,我在降级到EFCorefrom后出现错误5.03.1.我认为这个错误是由于我试图用DBSet<Entity_StuffINeed>. 实际上,清洁和重建解决了这个问题。上下文的错误。

System.TypeLoadException:来自程序集“Microsoft.EntityFrameworkCore.SqlServer,版本=3.1.13.0,文化=中性,PublicKeyToken=adb9793829ddae60”的“Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitorFactory”类型中的“方法“创建”没有一个实施。

原来的:

我正在连接到现有数据库并且无法编辑表。实体非常大。我使用脚手架来获取模型类。

public partial class Entity_Database
{
    public Entity_Database()
    {
        CollectionRef1 = new HashSet<CollectionRef1 >();
        ... (12 other collectiosn here)
        CollectionRef13 = new HashSet<CollectionRef13 >();
    }

    public int Prop1 { get; set; }
    ...(47 other props here)
    public int? Prop48 { get; set; }

    public virtual Ref1 Ref1 { get; set; }
    ...
    public virtual Ref12 Ref12 { get; set; }

    public virtual ICollection<CollectionRef1> CollectionRef1 { get; set; }
    ...
    public virtual ICollection<CollectionRef13> CollectionRef13 { get; set; }
}

但我只需要这些属性的一小部分。

public class Entity_StuffINeed
{
    public int Prop1 {get;set;}
    public string Prop4 {get;set;}
    public DateTime Prop8 {get;set;}
}

我可以使用 SQL,然后手动映射到我的对象:

SELECT Prop1, Prop4, Prop8 FROM Entity_Database;

但我找不到在 EFcore 中做类似事情的方法。

我的猜测是我使用了错误的工具。Dapper 不允许我非常喜欢的 Linq 语法。如果在 EFcore 中不可能,我应该使用什么?

标签: c#entity-framework-core

解决方案


推荐阅读