Tag: 存储库

LINQ,在映射中实现接口和exception的实体

我正在使用LINQ的存储库模式,有IRepository.DeleteOnSubmit(T Entity)。 它工作正常,但当我的实体类有接口时,如下所示: public interface IEntity { int ID {get;set;} } public partial class MyEntity: IEntity { public int ID { get { return this.IDfield; } set { this.IDfield=value; } } } 然后尝试删除这样的实体: IEntity ie=repository.GetByID(1); repoitory.DeleteOnSubmit(ie); 投 成员’IEntity.ID’没有支持的SQL转换。 从DB工作中获取数据,但删除和插入不起作用。 如何使用DataContext的接口? 这里是: exception消息:成员’MMRI.DAL.ITag.idContent’没有支持的SQL转换。 码: var d = repContent.GetAll().Where(x => x.idContent.Equals(idContent)); foreach (var tagConnect in d) <- […]

使用Ninject在我的存储库类中设置DBContext的连接字符串

我有一个使用EF 6的MVC 5应用程序,并使用DI容器Ninject实现具有dependency injection的Repository模式。 dbcontext的连接字符串存储在EF Context正确找到的Web.config文件中。 一切正常。 最近,我要求需要在运行时确定与我的DBContext的连接并连接到不同的数据库(但具有完全相同的结构)。 因此,我需要在实例化存储库之前在运行时从实体连接字符串更改sql connectionstring部分。 我真的很感激帮助。 我不是DI大师; 知道足够的Ninject让我的事情继续下去。 这是我的存储库基础接口: public interface IRepositoryBase where T : class { void Add(T entity, string userGuid = “”); void Delete(T entity); // … removed other method signatures for brevity } 我的存储库基础实现: public abstract class RepositoryBase : IRepositoryBase, IDisposable where T : class where D […]

如何使用简单的注入器,存储库和上下文 – 首先编码

我正在尝试使用Simple Injector来创建我的存储库并在业务逻辑层中使用它(我也想使用PerWebRequest方法)。 在DAL层我有: public interface IRepository where T : class { void Add(T entity); void Delete(T entity); void Delete(int id); void Update(T entity); T GetById(int Id); IQueryable All(); IEnumerable Find(Func predicate); } 并且: public class EFRepository : IRepository, IDisposable where T : class { #region Members protected DbContext Context { get; set; } protected DbSet […]

entity framework+存储库+工作单元

我正在考虑使用EF 4开始一个新项目并浏览一些文章,我发现了一些关于EF的文章和存储库模式以及工作单元 ( http://tdryan.blogspot.com/2011/03/another-entity-framework-4-repository_15.html和http://blogs.msdn.com/b/adonet/archive/2009/06/16/using -repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx ) 我正在使用第一个(第1部分,第2部分和第3部分)。 它们非常相似。 在这种情况下,我是新手。 我在这两个post之间感到困惑。 我已经创建了所有内容,但我不知道如何开始使用上下文并添加一些实体。 我发布了第二个链接,因为发布了实现它的方法。 ObjectContext派生自IUnitOfWork ,所以我很难选择哪两个更好用。

AutoMapper.Map忽略源对象的所有Null值属性

我正在尝试映射2个相同类型的对象。 我想要做的是AutoMapper to toigonore所有属性,在源对象中具有Null值,并保留目标对象中的现有值。 我已经尝试在我的“存储库”中使用它,但它似乎不起作用。 Mapper.CreateMap().ForAllMembers(p => p.Condition(c => !c.IsSourceValueNull)); 可能是什么问题?

使用Mock IDbSet对Entity Framework进行unit testing

我以前从来没有真正完成过unit testing,而且在第一次测试时我遇到了绊倒和绊倒。 问题是_repository.Golfers.Count(); 始终表示DbSet为空。 我的测试很简单,我只想添加一个新的高尔夫球手 [TestClass] public class GolferUnitTest //: GolferTestBase { public MockGolfEntities _repository; [TestMethod] public void ShouldAddNewGolferToRepository() { _repository = new MockGolfEntities(); _repository.Golfers = new InMemoryDbSet(CreateFakeGolfers()); int count = _repository.Golfers.Count(); _repository.Golfers.Add(_newGolfer); Assert.IsTrue(_repository.Golfers.Count() == count + 1); } private Golfer _newGolfer = new Golfer() { Index = 8, Guid = System.Guid.NewGuid(), FirstName = “Jonas”, […]