repository-pattern

How to de-attach an entity from a Context in Entity Framework?

↘锁芯ラ 提交于 2019-11-27 04:03:48
问题 I use EF 4.1 with Repository and DbContext.. POCO with T4 template. For every Repository I use a separate DbContext. I need to update an object with has a related property, at the moment I receive this error An entity object cannot be referenced by multiple instances of IEntityChangeTracker. I suppose my problem is beacuse eventObj and candidate are created from different Repositories. So i'm trying to solve the problem with this code, with no success. My question? How do I get rid of this

Pure POCO entity update problem in repository pattern

女生的网名这么多〃 提交于 2019-11-27 02:33:20
问题 I have a problem in my UserRepository in which I want to update a user. I dont want certain fields updated, such as password, unless specified. For example, When I pass the User from the view, to the service to the repository, it sends up the user with a null or empty password string. This null gets written to the database (which I dont want). How do I handle a situation like this? Domain public class User { public int UserId { get; set; } public string Email { get; set; } public string

How to write Repository method for .ThenInclude in EF Core 2

a 夏天 提交于 2019-11-27 02:12:52
问题 I'm trying to write a repository method for Entity Framework Core 2.0 that can handle returning child collections of properties using .ThenInclude, but I'm having trouble with the second expression. Here is a working method for .Include, which will return child properties (you supply a list of lambdas) of your entity. public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties) { IQueryable<T> query = _context.Set<T>(); foreach (var

Is this Repository pattern efficient with LINQ-to-SQL?

一曲冷凌霜 提交于 2019-11-27 02:01:17
问题 I'm currently reading the book Pro Asp.Net MVC Framework. In the book, the author suggests using a repository pattern similar to the following. [Table(Name = "Products")] public class Product { [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] public int ProductId { get; set; } [Column] public string Name { get; set; } [Column] public string Description { get; set; } [Column] public decimal Price { get; set; } [Column] public string Category { get; set; } }

Implementing retry logic for deadlock exceptions

拥有回忆 提交于 2019-11-27 01:29:13
问题 I've implemented a generic repository and was wondering if there is a smart way to implement a retry logic in case of a deadlock exception? The approach should be the same for all repository methods. So is there anyway I can avoid writing 'try/catch - call method again with retry-count', in every single method? Any suggetsion are welcome. A bit of my Repository code: public class GenericRepository : IRepository { private ObjectContext _context; public List<TEntity> ExecuteStoreQuery<TEntity>

Using the repository pattern to support multiple providers

落花浮王杯 提交于 2019-11-27 00:47:55
问题 Well, not sure if that's exactly the right title, but basically I have been having a lot of problems using repositories in MVC applications in such a way that you can substitute one set of repositories, implementing a different data storage technology, for another. For example, suppose I want to use Entity Framework for my application. However, I also want to have a set of test data implemented in hard-coded Lists. I would like to have a set of interfaces (IUserRepository, IProductRepository,

Configuring Ninject with Asp.Net MVC & Web Api

杀马特。学长 韩版系。学妹 提交于 2019-11-27 00:35:12
问题 i have setup my project with Ninject IoC . My project has regular Asp.Net MVC controllers and Web Api controllers. Now, Ninject works with Web Api but Ninject doesn't work with regular Asp.MVC controllers. My regular MVC controller implementation; public class GalleryController : BaseController { public GalleryController(IUow uow) { Uow = uow; } ........ } Error when using with regular controller An error occurred when trying to create a controller of type 'Web.Controllers.HomeController'.

Looking for a Ninject scope that behaves like InRequestScope

孤人 提交于 2019-11-26 23:16:07
On my service layer I have injected an UnitOfWork and 2 repositories in the constructor. The Unit of Work and repository have an instance of a DbContext I want to share between the two of them. How can I do that with Ninject ? Which scope should be considered ? I am not in a web application so I can't use InRequestScope . I try to do something similar... and I am using DI however, I need my UoW to be Dispose d and created like this. using (IUnitOfWork uow = new UnitOfWorkFactory.Create()) { _testARepository.Insert(a); _testBRepository.Insert(b); uow.SaveChanges(); } EDIT: I just want to be

Entity Framework Repository Pattern why not return Iqueryable?

不羁岁月 提交于 2019-11-26 21:36:42
问题 There are several good blogs about how to implement the repository pattern together with the unit-of-work pattern using generic classes. Implementing a Data Access Layer with Entity Framework 6.1 Implementing the Repository and Unit of Work Patterns The Idea is, to define a generic interface IRepository and a class Repository that hides how the data is actually accessed. It can be accessed using Entity Framework DbContext, or maybe the repository is an in memory collection for unit testing.

generic repository EF4 CTP5 getById

北慕城南 提交于 2019-11-26 21:30:43
问题 I have a generic repository an I am trying to add a GetById method as shown here C# LINQ to SQL: Refactoring this Generic GetByID method The problem is my repository does not use System.Data.Linq.DataContext instead I use System.Data.Entity.DbContext So I get errors where I try to use Mapping.GetMetaType and return _set.Where( whereExpression).Single(); How can I implement a generic GetById method in CTP5? Should I be using System.Data.Entity.DbContext in my Repository. Here is the start of