Tag: dbcontext

如何在使用EF代码的.SaveChanges()期间记录所有实体更改?

我先使用EF代码 。 此外,我正在为我的所有存储库和注入存储库的IUnitofWork使用基本存储库: public interface IUnitOfWork : IDisposable { IDbSet Set() where TEntity : class; int SaveChanges(); } public class BaseRepository where T : class { protected readonly DbContext _dbContext; protected readonly IDbSet _dbSet; public BaseRepository(IUnitOfWork uow) { _dbContext = (DbContext)uow; _dbSet = uow.Set(); } //other methods } 例如,我的OrderRepository是这样的: class OrderRepository: BaseRepository { IUnitOfWork _uow; […]

EntitySet System.InvalidOperationException – “实体类型不是当前上下文模型的一部分”

类似的问题 实体类型不是当前上下文的模型的一部分 – 并且 – EF 4.1 Code First错误 – 实体类型SomeType不是当前上下文的模型的一部分是类似的问题,但它们是“代码优先”透视图只有更简单的数据模型,并解决连接字符串和映射问题。 请仔细看看这个。 症状 // HomeController.cs public ActionResult Index() { var _db = new MealsContext(); var m = _db.Meals.ToList(); var d = _db.Drinks.ToList(); return View(); } 检索Drinks集合时会抛出exception: The entity type Drink is not part of the model for the current context. 码 // Meal.cs public class […]

EF5 db.Database.SqlQuery映射返回的对象

我有两个C#类 public class SearchResult { public int? EntityId { get; set; } public string Name { get; set; } public Address RegisteredAddress { get; set; } } 和 public class Address { public int? AddressId { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string […]

你能从DbSet获得DbContext吗?

在我的应用程序中,有时需要在一次操作中将10,000行或更多行保存到数据库中。 我发现,简单地迭代并逐个添加每个项目可能需要花费半个多小时。 但是,如果我禁用AutoDetectChangesEnabled它需要约5秒(这正是我想要的) 我正在尝试向DbSet创建一个名为“AddRange”的扩展方法,该方法将禁用AutoDetectChangesEnabled,然后在完成时重新启用它。 public static void AddRange(this DbSet set, DbContext con, IEnumerable items) where TEntity : class { // Disable auto detect changes for speed var detectChanges = con.Configuration.AutoDetectChangesEnabled; try { con.Configuration.AutoDetectChangesEnabled = false; foreach (var item in items) { set.Add(item); } } finally { con.Configuration.AutoDetectChangesEnabled = detectChanges; } } 所以,我的问题是:有没有办法从DbSet获取DbContext? 我不喜欢把它作为参数 – 感觉它应该是不必要的。

如何模拟DbContext

这是我想要测试的代码 public DocumentDto SaveDocument(DocumentDto documentDto) { Document document = null; using (_documentRepository.DbContext.BeginTransaction()) { try { if (documentDto.IsDirty) { if (documentDto.Id == 0) { document = CreateNewDocument(documentDto); } else if (documentDto.Id > 0) { document = ChangeExistingDocument(documentDto); } document = _documentRepository.SaveOrUpdate(document); _documentRepository.DbContext.CommitChanges(); } } catch { _documentRepository.DbContext.RollbackTransaction(); throw; } } return MapperFactory.GetDocumentDto(document); } 这是我的测试代码 [Test] public […]

使用DbContext设置()而不是在上下文中公开

执行以下操作时是否存在任何差异: public class UsersContext : DbContext { public DbSet Users { get; set; } } 与使用上下文的Set方法: public class UsersContext : DbContext { } var db = new UsersContext(); var users = db.Set(); 这些有效地做了同样的事情,给了我一组用户,但是除了你没有通过属性暴露集合之外还有什么大的差别吗?

一个具有多个dbcontexts的事务

我在unit testing中使用事务来回滚更改。 unit testing使用dbcontext,我正在测试的服务使用自己的。 它们都包装在一个事务中,一个dbcontext在另一个事务的块中。 问题是,当内部dbcontext保存他的更改时,外部dbcontext不可见(我不认为这是因为另一个dbcontext可能已经加载了对象)。 这是一个例子: [TestMethod] public void EditDepartmentTest() { using (TransactionScope transaction = new TransactionScope()) { using (MyDbContext db = new MyDbContext()) { //Arrange int departmentId = (from d in db.Departments where d.Name == “Dep1” select d.Id).Single(); string newName = “newName”, newCode = “newCode”; //Act IDepartmentService service = new DepartmentService(); service.EditDepartment(departmentId, newName, […]

IDbSet.Add和DbEntityEntry.State = EntityState.Added有什么区别?

在EF 4.1+中,这两行代码之间有区别吗? dbContext.SomeEntitySet.Add(entityInstance); dbContext.Entry(entityInstance).State = EntityState.Added; 或者他们做同样的事情? 我想知道是否可能会影响子集合/导航属性与另一个不同。

配置多个数据库entity framework6

在我的解决方案中,我有两个使用Entity Framework 6的项目。每个项目指向不同的数据库,两者都使用相同的数据提供 – SQL Server。 我的解决方案中的第三个项目需要使用这两个数据库。 我的问题是如何配置这些上下文。 我尝试在单独的程序集中创建配置类: namespace OSAD_Base { class EfDbConfiguration : DbConfiguration { public EfDbConfiguration() { SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance); } } } 并在每个上下文类中引用此配置: namespace IntegrationDb { [DbConfigurationType(“OSAD_Base.EfDbConfiguration, OSAD_Base”)] public partial class IntegrationEntities : DbContext { public IntegrationEntities(string connectionString) : base(connectionString) { } } } 初始化我的第一个时,所有工作都正确,但是当第二个上下文初始化时(顺序无关紧要)我得到并且错误: 设置了’EfDbConfiguration’的实例,但是在与’B1Entities’上下文相同的程序集中未发现此类型。 将DbConfiguration类型放在与DbContext类型相同的程序集中,在DbContext类型上使用DbConfigurationTypeAttribute指定DbConfiguration类型,或在配置文件中设置DbConfiguration类型。 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260883。* 我还试图在我的app.config(启动项目)中创建一个entityframework部分,但是出现了以下错误: 配置系统无法初始化 无法识别的配置部分entityFramework 如何在同一解决方案中使用2个独立的EF项目?

由于已使用MVC 4处理了DbContext,因此无法完成操作

我知道这个问题被问过这么多次。 我已阅读并实施了所有解决方案但未获得成功。 当我使用EF从数据库检索数据并在View上使用此模型后与模型绑定时,我收到此错误。 我的控制器代码是 using System.Linq; using System.Web.Mvc; using JsonRenderingMvcApplication.Models; namespace JsonRenderingMvcApplication.Controllers { public class PublisherController : Controller { public ActionResult Index() { PublisherModel model = new PublisherModel(); using (DAL.DevelopmentEntities context = new DAL.DevelopmentEntities()) { model.PublisherList = context.Publishers.Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }); ; } return View(model); } } […]