dbcontext

How can I log all entities change, during .SaveChanges() using EF code first?

我们两清 提交于 2019-11-27 10:32:26
问题 I'm using EF code first . I'm using a base Repository for all my repositories and an IUnitofWork that inject to the repositories, too: public interface IUnitOfWork : IDisposable { IDbSet<TEntity> Set<TEntity>() where TEntity : class; int SaveChanges(); } public class BaseRepository<T> where T : class { protected readonly DbContext _dbContext; protected readonly IDbSet<T> _dbSet; public BaseRepository(IUnitOfWork uow) { _dbContext = (DbContext)uow; _dbSet = uow.Set<T>(); } //other methods } e

How to force Entity Framework to always get updated data from the database?

心已入冬 提交于 2019-11-27 10:14:10
问题 I am using EntityFramework.Extended library to perform batch updates. The only problem is EF does not keep track of the batch updates performed by the library. So when I query the DbContext again it does not return the updated entities. I found that using AsNoTracking() method while querying disables the tracking and gets fresh data from the database. However, since EF does not keep track of the entities queried with AsNoTracking() , I am not able to perform any update on the queried data. Is

How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

核能气质少年 提交于 2019-11-27 09:55:26
问题 I'm using the DbContext and Code First APIs introduced with Entity Framework 4.1. The data model uses basic data types such as string and DateTime . The only data annotation I'm using in some cases is [Required] , but that's not on any of the DateTime properties. Example: public virtual DateTime Start { get; set; } The DbContext subclass is also simple and looks like: public class EventsContext : DbContext { public DbSet<Event> Events { get; set; } protected override void OnModelCreating

How to convert DbSet in Entity framework to ObjectQuery

空扰寡人 提交于 2019-11-27 09:15:28
I am using CodeFirst approach and struck with an issue where I need to convert DbSet to ObjectQuery. This is what I did for conversion. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>(); where db is the context inheriting from DbContext and Request is class. So, when I try to call the method that expects ObjectQuery as ObjectQueryMethod(objectSet), it throws the following error. "Type of conditional expression cannot be determined because there is no implicit conversion between 'System.Data.Entity

Mocking EF DbContext with Moq

丶灬走出姿态 提交于 2019-11-27 06:57:17
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() { // Arrange var mock = new Mock<IDbContext>(); mock.Setup(x => x.Set<User>()) .Returns(new List<User> { new

EntitySet System.InvalidOperationException - “the entity type is not part of the model for the current context”

大兔子大兔子 提交于 2019-11-27 06:55:22
问题 Similar Questions The entity type <classname> is not part of the model for the current context -and- EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context are similar questions but they are "code first" perspective only, with much simpler data models, and address connection string and mapping issues. Please look closely at this one. Symptom // HomeController.cs public ActionResult Index() { var _db = new MealsContext(); var m = _db.Meals.ToList();

EF Code First DBContext and Transactions

五迷三道 提交于 2019-11-27 06:03:55
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? Ladislav Mrnka Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { //

EF5 db.Database.SqlQuery mapping returned objects

半世苍凉 提交于 2019-11-27 06:00:59
问题 I have two C# classes public class SearchResult { public int? EntityId { get; set; } public string Name { get; set; } public Address RegisteredAddress { get; set; } } and public class Address { public int? AddressId { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Address3 { get; set; } } this is used in a dbContext call to map out the returning objects from a database via EF5 using (DbEntities db = new DbEntities()) { querySearchResult =

One transaction with multiple dbcontexts

梦想与她 提交于 2019-11-27 04:21:55
问题 I am using transactions in my unit tests to roll back changes. The unit test uses a dbcontext, and the service i'm testing uses his own. Both of them are wrapped in one transaction, and one dbcontext is in the block of the other. The thing is, when the inner dbcontext saves his changes, it's not visible to the outer dbcontext (and i don't think it's because the other dbcontext might already have the object loaded). Here is the example: [TestMethod] public void EditDepartmentTest() { using

How change tracking works in Entity Framework

家住魔仙堡 提交于 2019-11-27 04:15:17
问题 Given the following code, how does EF/DbContext knows about the change made to the customer object: class Program { static void Main() { using(var shopContext = new ShopContext()) { var customer = shopContext.Customers.Find(7); customer.City = "Marion"; customer.State = "Indiana"; shopContext.SaveChanges(); } } } public class ShopContext : DbContext { public DbSet<Customer> Customers { get; set; } } public class Customer { public int Id { get; set; } public string FirstName { get; set; }