dbcontext

How to Refresh DbContext

戏子无情 提交于 2019-11-27 03:58:51
I want to refresh all entities of my DbContext without recreating it, I tried the following and none of them make sense: var context = ((IObjectContextAdapter)myDbContext).ObjectContext; var refreshableObjects = (from entry in context.ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged) where entry.EntityKey != null select entry.Entity); context.Refresh(RefreshMode.StoreWins, refreshableObjects); //....................................................................... foreach (var entry in this.Orm.ChangeTracker

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

混江龙づ霸主 提交于 2019-11-27 03:34:35
I have the following code (Database is SQL Server Compact 4.0): Dim competitor=context.Competitors.Find(id) When I profile this the Find method takes 300+ms to retrieve the competitor from a table of just 60 records. When I change the code to: Dim competitor=context.Competitors.SingleOrDefault(function(c) c.ID=id) Then the competitor is found in just 3 ms. The Competitor class: Public Class Competitor Implements IEquatable(Of Competitor) Public Sub New() CompetitionSubscriptions = New List(Of CompetitionSubscription) OpponentMeetings = New List(Of Meeting) GUID = GUID.NewGuid End Sub Public

Entity Framework IValidatableObject reference DbContext

断了今生、忘了曾经 提交于 2019-11-27 03:33:46
问题 I'm trying to get EF 4.1 working with Repository, UnitOfWork, separation of entities from EF and validation. I followed this guide to get a nice separation of my POCO entities from the EF model and I'm now following this guide to implement validation (with IValidatableObject). My solution consists of: Contacts.Repository [references EF and Contacts.Entities]: Contacts.edmx ContactsDbContext.cs Contacts.Entities [no references]: Contact.cs (Contacts.Entities.Contact partial class) Contacts

DbContext discard changes without disposing

人盡茶涼 提交于 2019-11-27 03:11:11
I have a desktop client application that uses modal windows to set properties for hierarchical objects. Since this is a client application and access to the DbContext is not threaded, I use a long-running context on the main Form that gets passed around to modal children. These modal windows use the PropertyGrid to display entity properties and also have cancel buttons. If any data is modified and the cancel button is pressed, the changes are reflected in the parent form (where I cannot dispose the DbContext object ). Is there a way to discard any changes made if the DbContext.SaveChanges()

Entity Framework: Navigation Properties Issue

别等时光非礼了梦想. 提交于 2019-11-27 02:55:33
问题 I am working with Entity Framework code-first, and I have a class Course which has a navigation property Students : public virtual Collection<Student> Students { get; set;} It works ok, but as I access this navigation property, all the data is retrieved from the database: var allStudents = course.Students; // Here it retrieves the data var activeStudents = allStudents.Where(n => n.Active); // Here it filter the data on memory var listOfActiveStudents = activeStudents.ToList(); // It already

EF 4.1 OnModelCreating not called

牧云@^-^@ 提交于 2019-11-27 02:39:56
问题 I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string: <add name="AcmeDBContext" connectionString="metadata=res://*/|res://*/|res://*/; provider=System.Data.SqlClient; provider connection string=" data source=[server]; initial catalog=Acme;integrated security=True; multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> Here is my class inherited from

Using INotifyPropertyChanged with Entity Framework 6 DbContext Generator

我与影子孤独终老i 提交于 2019-11-27 02:29:59
问题 I know I can use ObjectContext instead, but I like the features of DbContext / DbSet. My application isn't large enough to warrant me writing complex view models, so I'd like to implement change notification on the EF generated models directly. How can I achieve this? 回答1: I've had great success using a NuGet package called PropertyChanged.Fody to get INPC implemented on the entity classes. Just install the package, then add the [ImplementPropertyChanged] attribute to any class and

Using Automapper to update an existing Entity POCO

时间秒杀一切 提交于 2019-11-27 01:37:00
问题 I am using EF4 DbContext to provide the model for an ASP.NET MVC app. I use ViewModels to provide data to the views and Automapper to perform the mapping between the EF POCOs and the ViewModels. Automapper does a great job but I'm not clear the best way to use it after the ViewModel is posted back to the controller to carry out an update. My idea is to get POCO object using a key contained in the ViewModel. I then want to use Automapper to update the POCO with data from the ViewModel:

How to set CommandTimeout for DbContext?

浪子不回头ぞ 提交于 2019-11-27 00:33:08
问题 I am looking a way to set CommandTimeout for DbContext. After searching I found the way by casting DbContext into ObjectContext and setting value for CommandTimeout property of objectContext. var objectContext = (this.DbContext as IObjectContextAdapter).ObjectContext; But I have to work with DbContext. 回答1: It will work with your method. Or subclass it (from msdn forum) public class YourContext : DbContext { public YourContext() : base("YourConnectionString") { // Get the ObjectContext

DbContext is very slow when adding and deleting

本秂侑毒 提交于 2019-11-27 00:20:52
问题 When using DbContext in a database-first scenario I found out that adding and deleting entities is very slow compared to ObjectContext. If adding 2000 entities and saving the changes at the end, DbContext is 3 to 5 times slower than ObjectContext (btw.: I know that adding a large amount of entities would be better using SqlBulkCopy but that's not the point). If saving changes after each addition DbContext is still nearly two times slower. When it comes to deletion it even gets worse: When