objectcontext

Calling SP from EF using same transaction of SaveChanges

谁说我不能喝 提交于 2019-11-30 10:15:20
Does someone knows how to call a StoredProc using the same transaction of an objectContext SaveChanges method (EntityFramework 5)? The goal is to apply the objects changes and call a stored Proc that does some "magic" on the DB, but, if something goes wrong (either with the SaveChanges or with the SP execution) no changes would be committed at all. Steps: Create the context get the connection from the context Create the transaction (TransactionScope) Open the connection (will enlist the connection into the ambient transaction created in 3. and will prevent from closing the connection by the

Entity Framework Object Context in ASP.NET Session object?

大城市里の小女人 提交于 2019-11-30 07:38:46
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 for longer on the server. A suggestion was made to keep an instance in the HttpContext.Current.Items

How to bind EF Code First DbContext to an Asp.Net DataSource?

混江龙づ霸主 提交于 2019-11-30 07:24:35
I've created the following Context to be used with Entity Framework Code First : public class Context : DbContext { public DbSet<Animal> Animals { get; set; } } Now I would like to use this Context in an Asp.Net application to perform CRUD operations using a GridView . I need to create a DataSource to do the data binding. How would I go about? The ASP part would look like this: <asp:GridView runat="server" DataSourceID="animalDataSource" DataKeyNames="AnimalID" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:CommandField

Reuseable ObjectContext or new ObjectContext for each set of operations?

假如想象 提交于 2019-11-30 05:31:33
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.SaveChanges(); } } Or this? public class DataAccess{ public DataAccess(){ } public IEnumerable<SomeItem>

Entity Framework 4.1 - Refresh is not a member of Context

元气小坏坏 提交于 2019-11-30 03:20:32
问题 I'm trying to revert Context changes using the Context.Refresh method but It seems like Refresh is not a member of Context. I'm using the Microsoft ADO.NET Entity Framework 4.1 RC version. Any idea? 回答1: You are likely looking at a DbContext, which does not have a Refresh method. You can use the IObjectContextAdapter interface to get the underlying ObjectContext , and call Refresh on that. var objectContext = ((IObjectContextAdapter)context).ObjectContext; 回答2: You can also use the Reload

C# Entity Framework using only one ObjectContext per HttpContext

天涯浪子 提交于 2019-11-29 21:26:20
问题 In ASP.NET MVC 2, using Entity Framework 4, I'm getting this error "An entity object cannot be referenced by multiple instances of IEntityChangeTracker". A search of SO shows that it is probably because I have different instances of the Entity Framework ObjectContext, when it should be only one ObjectContext instance for each HttpContext. I have this code (written long before I joined) that appears to do just that - have one ObjectContext for every HttpContext. But I am getting the

Calling SP from EF using same transaction of SaveChanges

别来无恙 提交于 2019-11-29 15:23:27
问题 Does someone knows how to call a StoredProc using the same transaction of an objectContext SaveChanges method (EntityFramework 5)? The goal is to apply the objects changes and call a stored Proc that does some "magic" on the DB, but, if something goes wrong (either with the SaveChanges or with the SP execution) no changes would be committed at all. 回答1: Steps: Create the context get the connection from the context Create the transaction (TransactionScope) Open the connection (will enlist the

Entity Framework Detach an entity and the related entities gone

梦想的初衷 提交于 2019-11-29 15:23:17
问题 When I use Entity Framework, I want to query out a record in a context and add it to another context with the same schema, after query out the record, I detach it from the context, but the related entities are all away, is there any way to solve it? Thanks in advance! 回答1: This is "by design". EF can detach entities only one by one but in the same time EF doesn't support object graphs composed of attached and detached entities. Because of that when you detach entity it will break all

ASP.Net Entity Framework, objectcontext error

旧街凉风 提交于 2019-11-29 13:19:15
I'm building a 4 layered ASP.Net web application. The layers are: Data Layer Entity Layer Business Layer UI Layer The entity layer has my data model classes and is built from my entity data model (edmx file) in the datalayer using T4 templates (POCO). The entity layer is referenced in all other layers. My data layer has a class called SourceKeyRepository which has a function like so: public IEnumerable<SourceKey> Get(SourceKey sk) { using (dmc = new DataModelContainer()) { var query = from SourceKey in dmc.SourceKeys select SourceKey; if (sk.sourceKey1 != null) { query = from SourceKey in

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

徘徊边缘 提交于 2019-11-29 12:19:54
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 call ObjectContext.SaveChanges() , an UpdateException is thrown with InnerException.Message value: