objectcontext

ObjectContext.SaveChanges() violates primary key, throws UpdateException?

非 Y 不嫁゛ 提交于 2019-12-18 07:09:31
问题 Paraphrasing from this MSDN documentation ... An INSERT statement is generated by the Entity Framework and executed on the data source when SaveChanges is called on the ObjectContext. If the INSERT operation succeeds, server-generated values are written back to the ObjectStateEntry. When AcceptChanges is called automatically at the end of the SaveChanges execution, a permanent EntityKey is computed by using the new server-generated values. This does not seem to be occurring in my code. When I

'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

I can't save data using Entity Framework

余生颓废 提交于 2019-12-13 16:15:41
问题 I use this code to add one item to a SpareParts table in my database. try { Database1Entities _db = new Database1Entities(); SparePart sparePart = new SparePart(); sparePart.manufactureID = 2; sparePart.name = "detail"; _db.SpareParts.AddObject(sparePart); int changesNumber = _db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave); int count = _db.SpareParts.Count(); int i = 8 + 1; } catch (Exception e) { Console.WriteLine(e.Message); } I tried _db.SaveChanges() . That didn

How to add/remove many-to-many relation with entity framework by entity key?

跟風遠走 提交于 2019-12-12 20:54:38
问题 I tried: using (Entities e = new Entities()) { EntityKey key = new EntityKey("Entities.Users", "UserId", 20); User user = new User { EntityKey = key}; Role role = e.Roles.FirstOrDefault(); //role.Users.Attach(user); //throws (when uncommented...) InvalidOperationException: //The object being attached to the source object is not attached to the same ObjectContext as the source object. role.Users.Add(user); //throws InvalidOperationException too: //The object cannot be added to the

How to clear contents of ObjectContext in Entity Framework 1.0

瘦欲@ 提交于 2019-12-12 05:39:09
问题 Is there a method of manually clearing/resetting an ObjectContext back to its initial state? Note that I can't just instantiate a new context. This is using the 1.0 version of the Entity Framework. Thanks 回答1: ObjectContext is meant to be a short-lived object, it shouldn't be cached like that. Normal usage should look like: using(var ctx = new MyContext()) { // Select/update/insert/delete ctx.SaveChanges(); } 回答2: You can try to call the Detach method for each entity that was loaded to the

EF4.0 - Is there a way to see what entities are attached to what ObjectContext during debugging?

☆樱花仙子☆ 提交于 2019-12-12 04:38:39
问题 This is in continuation with my problem here. I'm trying to use the solution Julie Lerman gave me a few months ago. I'm currently using the following to generate a new Game entity pre-attached to my ObjectContext: Game game = _gameRepository.GetGame(formData.GameID); AutoMapper.Mapper.Map<AdminGameEditModel, Game>(formData, game); In the repository, I try to attach the Game to the OC with its state set to 'Added' like she suggested by doing the following: public Game GetGame(int id) { if (id

Entity Framework Get Entity By Primary Key

ⅰ亾dé卋堺 提交于 2019-12-12 03:36:03
问题 Some time ago, I wanted to implement a method that was able to determine whether do an insert or an update on a given entity, so I didn't have to expose "Insert" and "Update" methods, but just a simple "InsertOrUpdate". The part of code that finds out if the entity is new or not, is this: public virtual T GetEntityByPrimaryKey<T>(T entity) where T : class { var entityType = entity.GetType(); var objectSet = ((IObjectContextAdapter)this.DatabaseContext).ObjectContext.CreateObjectSet<T>(); var

How to delete an associated object in Entity Framework without having access to the object context

…衆ロ難τιáo~ 提交于 2019-12-11 12:22:53
问题 Having two models, Site and Link, where a site has many links, how do I delete a link from inside a method of Site, which doesn't have access to the object context? I've tried something like: public void DeleteFirstLink() { var link = LinkSet.First(); LinkSet.Remove(link); } but it seems that is not really deleting the link, but breaking the association. Since there's a database constraints it throws this error: A relationship is being added or deleted from an AssociationSet 'Sites_have_Links

What library does ObjectContext come from in VB5?

限于喜欢 提交于 2019-12-11 03:41:13
问题 First of all, let me myself make the obligatory snarky comment about having to maintain VB5 code: yes, it's pitiful, but can we please just cut to the chase? Thanks. I am having to revisit some VERY old code that was written in 1998 and which hasn't been touched since 2003. The problem I am having is that I am getting a compile error on this: Dim ObjCtx As ObjectContext The VB5 compiler throws a compile error: User-defined type not defined This is not of course a user-defined type, but is a

ObjectMaterialize in EF not firing on first level query

邮差的信 提交于 2019-12-11 03:05:20
问题 I have a query such as: Query Syntax 1 - Does not fire the somehandler; var results = (from I in db.mytable select new myObject() { column1 = i.Prop1 }).ToList(); Query Syntax 2 - Does fires the somehandler event; var results = (from I in db.mytable select I).toList(); in my ContextClass I have something like this: ((IOjectContextAdapter)this).ObjectContext.ObjectMaterialized += somehandler; The only difference I see is that the first query builds a new object from the select results. Any