objectcontext

Entity Framework and Object Context lifetime in ASP.NET MVC

爷,独闯天下 提交于 2019-12-04 10:45:25
I'm using Entity Framework in my project, and I have the problem that, once I pass my entities to a View (keep in mind that these entities have lazy-initialized objects along the lines of: Products.Owner, where owner is an object that is lazily initialized) I get a run-time exception telling me that the ObjectContext is out of scope. Now this makes sense since I am getting the entities from a Service with a using (.... entities...) { .... } statement, which means it is disposed when the result is returned. How would I get around this and have an Object Context that is alive from start to end.

Entity Framework - refresh objects from database

China☆狼群 提交于 2019-12-03 11:54:02
I'm having trouble with refreshing objects in my database. I have an two PC's and two applications. On the first PC, there's an application which communicates with my database and adds some data to Measurements table. On my other PC, there's an application which retrives the latest Measurement under a timer, so it should retrive measurements added by the application on my first PC too. The problem is it doesn't. On my application start, it caches all the data from database and never get new data added. I use Refresh() method which works well when I change any of the cached data, but it doesn't

Why does Entity Framework automatically use the ObjectContext instead of the DbContext when mapping database tables using ADO.NET Entity datamodel

你。 提交于 2019-12-03 07:24:39
问题 I am following the database approach first; I have created the tables in my SQL Server 2008 database, then I map those tables to Entity Framework classes using an ADO.NET Entity Data Model. But when I opened the designer.cs file I found the following code in the class definition which was created automatically: public partial class PortalEntities : ObjectContext so I have the following three question that get my confused: Why does my PortalEntities class derive from ObjectContext and not

Entity Framework ObjectContext with Dependency Injection

房东的猫 提交于 2019-12-01 14:37:33
Well, it seems like I'm stuck in my application structure. Here's what I want to do: UI layer: An ASP.NET webforms website. BLL: Business logic layer which calls the repositories on DAL. DAL: .EDMX file (Entity Model) and ObjectContext with Repository classes which abstract the CRUD operations for each entity. Entities: The POCO Entities. Persistence Ignorant. Generated by Microsoft's ADO.Net POCO Entity Generator. I'd like to create an obejctcontext per HttpContext in my repositories to prevent performance/thread [un]safety issues. Ideally it would be something like: public MyDBEntities ctx {

Managing Entity Framework ObjectContext in ASP.NET

谁都会走 提交于 2019-12-01 04:19:02
问题 I'm using the Entity Framework for an ASP.NET Web Forms application and I'm wondering how I should deal with ObjectContext and it's lifetime. For example, I have an InviteService class that manages invites such as creating and accepting invites. The class itself is in another project/namespace from the Web project. An InviteUsers() method creates Invite entities for a list of users, calls a repository to save them to the database and mails each user an invite link. The method is called from

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

痴心易碎 提交于 2019-12-01 04:12:21
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 I did the same steps you did to setup a EF model. your database.mdf file has the Copy to Output Directory set to Copy always , that means that

MVC 3 - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

删除回忆录丶 提交于 2019-11-30 21:33:00
I'm very new to C# and MVC in general and I've been creating my own little blog site as a test project. Although most things are working up to this point, I have had problems selecting multiple columns from LINQ queries. It was only after stumbling on a question on SO that I realised I could use the generated entities classes as strongly-typed models to handle this. I've needed this as I've been creating a database layer (which is also something I've not used before) and trying to pass anonymous types through that layer didn't work. I understand why that was the case, so I'm satisfied that I'm

MVC 3 - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

十年热恋 提交于 2019-11-30 17:12:55
问题 I'm very new to C# and MVC in general and I've been creating my own little blog site as a test project. Although most things are working up to this point, I have had problems selecting multiple columns from LINQ queries. It was only after stumbling on a question on SO that I realised I could use the generated entities classes as strongly-typed models to handle this. I've needed this as I've been creating a database layer (which is also something I've not used before) and trying to pass

EF4 - possible to mock ObjectContext for unit testing?

怎甘沉沦 提交于 2019-11-30 13:49:34
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? I'm unit testing EF4 easily without mocking. What I did was create a repository interface using the code from http://elegantcode.com/2009/12/15/entity-framework-ef4

How to refresh ObjectContext cache from db?

房东的猫 提交于 2019-11-30 11:31:38
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? The Refresh method is what you are looking for: Context.Refresh(RefreshMode.StoreWins, somethings); The EF data context is an implementation of the Unit of Work pattern. As such, it is NOT