dbcontext

Entity Framework and calling context.dispose()

試著忘記壹切 提交于 2019-11-26 15:12:46
When should one call DbContext.dispose() with entity framework? Is this imaginary method bad? public static string GetName(string userId) { var context = new DomainDbContext(); var userName = context.UserNameItems.FirstOrDefault(x => x.UserId == userId); context.Dispose(); return userName; } Is this better? public static string GetName(string userId) { string userName; using(var context = new DomainDbContext()) { userName = context.UserNameItems.FirstOrDefault(x => x.UserId == userId); context.Dispose(); } return userName; } Is this even better, that is, should one NOT call context.Dispose()

How can I log the generated SQL from DbContext.SaveChanges() in my Program? [duplicate]

不羁岁月 提交于 2019-11-26 15:07:51
This question already has an answer here: Entity Framework 4.0: How to see SQL statements for SaveChanges method 6 answers According this thread, we can log the generated SQL via EF , but what about DbContext.SaveChanges() ? Is there any easy way to do this job without any extra frameworks? In entity framework 6.0, the Database class has a property Action<string> Log . so setting up logging is as easy as: context.Database.Log = Console.WriteLine; For more advanced needs you can set up an interceptor . More info on the entity framework wiki See http://www.codeproject.com/Articles/499902

Mocking EF DbContext with Moq

僤鯓⒐⒋嵵緔 提交于 2019-11-26 13:00:21
问题 I\'m trying to create a unit test for my service with a mocked DbContext. I created an interface IDbContext with the following functions: public interface IDbContext : IDisposable { IDbSet<T> Set<T>() where T : class; DbEntityEntry<T> Entry<T>(T entity) where T : class; int SaveChanges(); } My real context implements this interface IDbContext and DbContext . Now I\'m trying to mock the IDbSet<T> in the context, so it returns a List<User> instead. [TestMethod] public void TestGetAllUsers() { /

How to Refresh DbContext

萝らか妹 提交于 2019-11-26 12:42:33
问题 I want to refresh all entities of my DbContext without recreating it, I tried the following and none of them make sense: var context = ((IObjectContextAdapter)myDbContext).ObjectContext; var refreshableObjects = (from entry in context.ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged) where entry.EntityKey != null select entry.Entity); context.Refresh(RefreshMode.StoreWins, refreshableObjects); //..................

The entity type <type> is not part of the model for the current context

删除回忆录丶 提交于 2019-11-26 12:09:58
I am getting into the Entity Framework, but I am unsure if I am missing a critical point in the code-first approach. I am using a generic repository pattern based on the code from https://genericunitofworkandrepositories.codeplex.com/ and have created my entities. But when I try to access or modify the entity I run into the following: System.InvalidOperationException: The entity type Estate is not part of the model for the current context. It happens when I am trying to access it from my repository: public virtual void Insert(TEntity entity) { ((IObjectState)entity).ObjectState = ObjectState

EF Code First DBContext and Transactions

谁说胖子不能爱 提交于 2019-11-26 11:51:42
问题 I would like know what is the best possible way to implement transactions with DBContext . In particular, Does DbContext.SaveChanges implement transaction internall if i change multiple entities? If i want to call DbContext.SaveChanges multiple times(same contxet/different contxets), how transaction can be achieved? 回答1: Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope

The operation cannot be completed because the DbContext has been disposed using MVC 4

谁说胖子不能爱 提交于 2019-11-26 11:08:32
问题 I know that this Question is asked so many times. I have read and implemented all solution but didn\'t get success. I am getting this error when I retrieve data from database using EF and binds with model after that use this model on View. My controller code is using System.Linq; using System.Web.Mvc; using JsonRenderingMvcApplication.Models; namespace JsonRenderingMvcApplication.Controllers { public class PublisherController : Controller { public ActionResult Index() { PublisherModel model =

DbContext discard changes without disposing

耗尽温柔 提交于 2019-11-26 10:26:31
问题 I have a desktop client application that uses modal windows to set properties for hierarchical objects. Since this is a client application and access to the DbContext is not threaded, I use a long-running context on the main Form that gets passed around to modal children. These modal windows use the PropertyGrid to display entity properties and also have cancel buttons. If any data is modified and the cancel button is pressed, the changes are reflected in the parent form (where I cannot

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

杀马特。学长 韩版系。学妹 提交于 2019-11-26 06:05:50
问题 I have the following generic extension method: public static T GetById<T>(this IQueryable<T> collection, Guid id) where T : IEntity { Expression<Func<T, bool>> predicate = e => e.Id == id; T entity; // Allow reporting more descriptive error messages. try { entity = collection.SingleOrDefault(predicate); } catch (Exception ex) { throw new InvalidOperationException(string.Format( \"There was an error retrieving an {0} with id {1}. {2}\", typeof(T).Name, id, ex.Message), ex); } if (entity ==

How can I log the generated SQL from DbContext.SaveChanges() in my Program? [duplicate]

ε祈祈猫儿з 提交于 2019-11-26 04:11:38
问题 This question already has answers here : Entity Framework 4.0: How to see SQL statements for SaveChanges method (6 answers) Closed last year . According this thread, we can log the generated SQL via EF , but what about DbContext.SaveChanges() ? Is there any easy way to do this job without any extra frameworks? 回答1: In entity framework 6.0, the Database class has a property Action<string> Log . so setting up logging is as easy as: context.Database.Log = Console.WriteLine; For more advanced