DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn’t. DetectChanges is an expensive operation, so that’s the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity). If you want to use … Read more

Adding a New Column to an Existing Table in Entity Framework

The “Update Model from Database” is hard/slow to use and is prone to errors. It generates other stuff that you probably don’t want/need. So manually adding the column that you need will work better. I suggest you do it outside the VS editor since depending on how many models/tables, it can be very slow opening … Read more

EF6.0 “The relationship could not be changed because one or more of the foreign-key properties is non-nullable”

You need to delete the ItemTypeItem. It is not possible to just remove it from the Items list as it cannot exist by itself, because it has a non-nullable foreign key referencing ItemType (ItemTypeID). To delete the ItemTypeItem add context.Entry(itemTypeItem).State = EntityState.Deleted;

Entity Framework: Invalid column name ‘OrganizationStructure_ID’

That is because you didn’t pair your FK property with a navigation property. I expect the ParentID should point to parent OrganizationStructure and ChildrenItems should point to children OranizationStructures. If your model doesn’t contain Parent navigation property to parent OrganizationStructure you must use fluent-API to tell EF that ParentID is FK: modelBuilder.Entity<OrganizationStructure>() .HasMany(o => o.ChildrenItems) … Read more

Using EF Core ThenInclude() on Junction tables

but I get an ICollection in the last lambda displayed, so I obviously need the Select() No, you don’t. EF Core Include / ThenInclude totally replace the need of Select / SelectMany used in EF6. Both they have separate overloads for collection and reference type navigation properties. If you use the overload with collection, ThenInclude … Read more