wpf EF 6.0 中缺少 DbSet<entity>.Load() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19605682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:57:27  来源:igfitidea点击:

DbSet<entity>.Load() function missing in EF 6.0

c#.netwpfentity-framework-6

提问by Hassan Akhtar

I am trying to access the DbSet<EntityClass>.Load()function to load the entities. This function no longer exists in EF 6.0; upon certain investigation I found that it is a part of the extension methods defined in the EF extension library.

我正在尝试访问该DbSet<EntityClass>.Load()函数以加载实体。EF 6.0 中不再存在此功能;经过某些调查,我发现它是 EF 扩展库中定义的扩展方法的一部分。

I get the reference NuGet Packages for EF 6.0 extended library but seems like it's no longer supported. I tried to do an alternative of that function by calling .ToList(), but this method upon processing returns me an inner exception:

我获得了 EF 6.0 扩展库的参考 NuGet 包,但似乎不再受支持。我试图通过调用来替代该函数.ToList(),但此方法在处理时返回一个内部异常:

({"The column name is not valid. [ Node name (if any) = Extent1,Column name = HasErrors ]"} )

({"The column name is not valid. [ Node name (if any) = Extent1,Column name = HasErrors ]"} )

I double checked the mapping class against the database table, but it looks fine. Not sure what I am missing. Below is the code of my mapping class:

我根据数据库表仔细检查了映射类,但看起来不错。不知道我错过了什么。下面是我的映射类的代码:

internal class CustomerMapping : EntityTypeConfiguration<Customer>
{
    public CustomerMapping()
    {
        this.HasKey(t => t.Id);

        this.Property(t => t.Id).HasColumnName("CUSTOMER_ID");
        this.Property(t => t.Name).HasMaxLength(30).HasColumnName("NAME");
        this.Property(t => t.Email).HasMaxLength(30).HasColumnName("EMAIL");
        this.Property(t => t.PhoneNo).HasMaxLength(100).HasColumnName("PHONE_NO");
        this.Property(t => t.MobileNo).HasMaxLength(100).HasColumnName("MOBILE_NO");
        this.Property(t => t.Address1).HasMaxLength(100).HasColumnName("ADDRESS1");
        this.Property(t => t.Address2).HasMaxLength(100).HasColumnName("ADDRESS2");
        this.Property(t => t.CustomerType).HasMaxLength(100).HasColumnName("CUSTOMER_TYPE");
        this.Property(t => t.Notes).HasMaxLength(100).HasColumnName("NOTES");

        this.ToTable("CUSTOMERS");
    }
}

Below is the actual call made to the database:

下面是对数据库的实际调用:

internal class EntityService : IEntityService
{
    private ObservableCollection<Customer> customers;


    public DBContextManager DataBaseContext { get; set; }

    public ObservableCollection<Customer> Customers
    {
        get
        {
            if (customers == null && DataBaseContext != null)
            {
               // DataBaseContext.Set<Customer>().Load()
                DataBaseContext.Set<Customer>().ToList();
                customers = DataBaseContext.Set<Customer>().Local;

            }
            return customers;
        }
    }
}

Also please can any one point out the difference between ToList()and Load()?

也请有人指出ToList()和之间的区别Load()吗?

回答by Paul Cookson

I found I needed to add:

我发现我需要添加:

using System.Data.Entity;

回答by Epsilon3

Also, besides System.Data.Entity, you must add the System.Linq namespace as well as System.Windows.

此外,除了 System.Data.Entity 之外,您还必须添加 System.Linq 命名空间以及 System.Windows。

回答by Pawel

In EF6 the class containing extension methods was renamed from DbQueryExtensions to QueryableExtensions but the .Load()method is still there. If you are not calling this extension method directly the rename should not matter to you.

在 EF6 中,包含扩展方法的类从 DbQueryExtensions 重命名为 QueryableExtensions,但该.Load()方法仍然存在。如果您不直接调用此扩展方法,则重命名对您来说无关紧要。

回答by Goran

DbSet.ToList() will return all items from given set, and will populate the DbSet.Local property. You can call either ToList() or Load(). You do not need to reference Local property though, you could create manually ObservableCollection.

DbSet.ToList() 将返回给定集合中的所有项目,并将填充 DbSet.Local 属性。您可以调用 ToList() 或 Load()。您不需要引用 Local 属性,您可以手动创建 ObservableCollection。

return new ObservbableCollection<Customer>(DataBaseContext.Set<Customer>().ToList());

There could be a difference between the ToList() and Local. If for example, this is not the first time you are executing a query on the customer set, Local could contain data that is invalid, if the data was deleted on the network.

ToList() 和 Local 之间可能存在差异。例如,如果这不是您第一次对客户集执行查询,如果数据在网络上被删除,则本地可能包含无效数据。