dbcontext

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

落爺英雄遲暮 提交于 2019-12-17 10:58:45
问题 In EF 4.1+, is there a difference between these 2 lines of code? dbContext.SomeEntitySet.Add(entityInstance); dbContext.Entry(entityInstance).State = EntityState.Added; Or do they do the same thing? I'm wondering if one might affect child collections / navigation properties differently than the other. 回答1: When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State =

'ObjectContext' vs 'DbContext' in Entity Framework

允我心安 提交于 2019-12-17 10:49:16
问题 I'm using the DbContext class within code that I am creating that is based on the Generic Repositories and Unit of Work design patterns. (I am following the guidance here.) While working on this project I have encountered the ObjectContext class. I've read quite a number of posts that discuss ObjectContext vs. DbContext . While some of what I've read makes sense, I still don't have a complete understanding of the differences and this leaves me wondering about my current implementation. Should

DbContext AutoDetectChangesEnabled set to false detecting changes

百般思念 提交于 2019-12-17 10:34:46
问题 I'm a bit stumped. From what I've read setting the DbContext.AutoDetectChangesEnabled to false should disable change tracking requiring one to call DbContext.DetectChanges in order to identify changes to be sent to the database. However, it is clear from my logs below that the changes are being registered by dbContexts change tracker, even with the setting set to false. Am I missing something? Entity Framework Version: 5.0.0.0 DbContext class public class ProjectContext : DbContext { public

How to convert DbSet in Entity framework to ObjectQuery

假装没事ソ 提交于 2019-12-17 09:53:57
问题 I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>(); where db is the context inheriting from DbContext and Request is class. So, when I try to call the method that expects ObjectQuery as ObjectQueryMethod(objectSet), it throws the following error. "Type of conditional

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

天涯浪子 提交于 2019-12-17 07:14:52
问题 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

WCF, OData, DbContext, and Joins

不羁岁月 提交于 2019-12-13 19:57:45
问题 I want to expose both simple entities and joins as OData via a single WCF service. Given a domain of Lists of Artists and their Albums, I know I can have a DbContext like so: public class PickContext: DbContext { public DbSet<Album> Albums { get; set; } public DbSet<Artist> Artists { get; set; } public DbSet<List> Lists { get; set; } and from that I can make an OData service like this: public class PickDataService : DataService<PickContext> { public static void InitializeService

Entity Framework: Controlling db connection and specifying own transaction

自古美人都是妖i 提交于 2019-12-13 19:06:58
问题 I want to find a way to control EF's underlying db connection & transaction to make sure my application is using only one connection at a time during one transaction (I will need to support both Oracle and SQL Server). I found this good article which comes with a lot of recommendations, but brings up (like all the other articles I have read) the TransactionScope . Well, I would like to stay away of TransactionScope , if possible... Could I have a solution for this playing with only pure

Changing Entity's State in Entity Framework 4.1

核能气质少年 提交于 2019-12-13 16:04:25
问题 Let say we have a Customer object which has an Order object. The Order object has OrderDetail object. Customer oCustomer using(var context = new MyContext) { oCustomer = context.Include("Order.OrderDetail").Find(1); } oCustomer.Name ="blah blah"; oCustomer.Order.Description = "blah blah"; oCustomer.Order.OrderDetail.Quantity = 10; Now when I change the state of Customer as following: using(var context = new MyContext) { context.Entry(oCustomer).State = EntityState.Modified. context

Entity Framework (4.2) HasRequired results in unexpected LEFT OUTER JOIN

爷,独闯天下 提交于 2019-12-13 11:35:44
问题 It appears that the Entity Framework (latest version from NuGet) may be ignoring the HasRequired configuration when constructing joins for Navigation Properties other than the first one defined. For example, given a POCO object (Person) with the following configuration: var person = modelBuilder.Entity<Person>(); person.ToTable("The_Peoples"); person.HasKey(i => i.Id); person.Property(i => i.Id).HasColumnName("the_people_id"); person.HasRequired(i => i.Address) .WithMany() .Map(map => map

Set identity ON on dbContext using method Set(Type).Add(entity)

雨燕双飞 提交于 2019-12-13 07:16:07
问题 I'm trying save entity to db using dbContext. Type entityType = Type.GetType("class"); object ob = db.Set(entityType).Create(entityType); ob.GetMethod("set_Id").Invoke(ob, new object[] { newId }); //...other set code... db.Set(entityType).Add(ob); db.SaveChanges(); -- here fires exception But after SaveChanges fire the Exception "Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF". In the profiler I see the standard insert batch with the