objectcontext

Entity Framework Update - The context is not currently tracking the entity

狂风中的少年 提交于 2020-01-04 04:12:07
问题 I am trying to update an entity but I am getting the following error: The context is not currently tracking the entity. My DB table consists of the following fields: fixturedate, leagueID (FK), Team A (FK), Team B (FK). My code is as follows: public void UpdateFixture(Fixture validFixture) { Fixture fixture = new Fixture(); fixture = entities.Fixtures.Where(f => f.fixtureId == validFixture.fixtureId).FirstOrDefault(); League league = new League(); league.LeagueId = validFixture.leagueId;

Reuseable ObjectContext or new ObjectContext for each set of operations?

拥有回忆 提交于 2019-12-30 01:31:12
问题 I'm new to the Entities Framework, and am just starting to play around with it in my free time. One of the major questions I have is regarding how to handle ObjectContexts. Which is generally preferred/recommended of these: This public class DataAccess{ MyDbContext m_Context; public DataAccess(){ m_Context = new MyDbContext(); } public IEnumerable<SomeItem> GetSomeItems(){ return m_Context.SomeItems; } public void DeleteSomeItem(SomeItem item){ m_Context.DeleteObject(item); m_Context

C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

自作多情 提交于 2019-12-28 12:44:06
问题 I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repository. What is the smartest and safest way to go about this? 回答1: A design with multiple ObjectContext instances is only acceptable if your Repository methods commit the transaction. Otherwise, it is possible that external calls to commit the transaction may not persist everything you intend, because you will hold references to different

Generic ObjectContext? objectContext.GetObjectSet<TEntity>?

只愿长相守 提交于 2019-12-25 00:43:23
问题 Is there a way to get ObjectQuery<T> for specfied generic type? Pseudo: public partial class MyObjectContext { public ObjectSet<TEntity> GetObjectSet<TEntity>() { return Helper.GetObjectSet<TEntity>(this); } } 回答1: Yes this is what you need: public partial class MyObjectContext { public ObjectSet<TEntity> GetObjectSet<TEntity>() { return this.CreateObjectSet<TEntity>(); } } As you can see your helper method is not needed because you can call CreateObjectSet directly on MyObjectContext

Does ObjectContext.Connection.BeginTransaction() use MSDTC?

十年热恋 提交于 2019-12-24 00:19:07
问题 I want confirm if the Transaction that Entity Framework's ObjectContext.Connection.BeginTransaction() method returns uses the support of MSDTC (Microsoft Distributed Transaction Coordinator) or not? Is there any way to use transactions without support of MSDTC? 回答1: It will automatically promote to a transaction coordinated by MSDTC under certain conditions. Each time you call ObjectContext.SaveChanges() a new transaction is created if there isn't already one in scope (if there is already an

Entity framework ObjectContext share - pros and cons

我的未来我决定 提交于 2019-12-23 09:24:57
问题 In my project I use entity framework 4.0 as ORM to persist data in a SQL Server. My project is a ribbon from application with a grid view and navigation tree in the main form with ribbon panel on top of it. My app basically acts a CRUD UI with very little business logic. Being first time with EF, I developed this project by creating and holding an instance of objectContext in the orchestrating form (main form or the one that shows up as application to user) as a member variable and bound a

How to add entity-framework to console application (images are included)

青春壹個敷衍的年華 提交于 2019-12-19 07:05:04
问题 I try to add entity-framework to console application: I press "add new item" and then then then I added code: class Program { static void Main(string[] args) { try { Database1Entities db = new Database1Entities(); db.AddToTableTest(new TableTest { name = "name" }); db.SaveChanges(); int count = db.TableTest.Count(); int ui = 9 + 0; } catch (Exception e) { } } } It gives no error, but I don't see any changes in database. I described the issue better here 回答1: I did the same steps you did to

EF4 - possible to mock ObjectContext for unit testing?

此生再无相见时 提交于 2019-12-18 16:49:39
问题 Can it be done without using TypeMock Islolator? I've found a few suggestions online such as passing in a metadata only connection string, however nothing I've come across besides TypeMock seems to truly allow for a mock ObjectContext that can be injected into services for unit testing. Do I plunk down the $$ for TypeMock, or are there alternatives? Has nobody managed to create anything comparable to TypeMock that is open source? 回答1: I'm unit testing EF4 easily without mocking. What I did

How to refresh ObjectContext cache from db?

喜夏-厌秋 提交于 2019-12-18 14:46:28
问题 We are loading data from db: var somethings = Context.SomethingSet.ToList(); Then someone deletes or adds rows outside of context. Out context still has caches deleted object, because it doesn't know they were deleted. Even if I call Context.SomethingSet.ToList(), our context still contains deleted objects and navigation properties are not correct. What is the best method to refresh whole set from database? 回答1: The Refresh method is what you are looking for: Context.Refresh(RefreshMode

Entity Framework Object Context in ASP.NET Session object?

瘦欲@ 提交于 2019-12-18 12:38:58
问题 We have a multi-layered Asp.NET Web Forms application. The data layer has a class called DataAccess which impements IDisposable and has an instance of our Entity Framework Object Context as a private field. The class has a number of public methods returning various collections of Entities and will dispose its Object Context when it is disposed. Due to a number of problems we've been facing, we decided it would be a big plus to keep the Object Context (or an instance of DataAccess ) in scope