简体   繁体   中英

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

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.

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:

({"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() ?

我发现我需要添加:

using System.Data.Entity;

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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