我是Entity Framework和MVC 4的新手,我一直在关注微软网站上的音乐商店应用教程(对我的需求进行了一些调整).我有一个使用Code First创建的远程数据库.这包括一个SampleData包含有关歌曲,艺术家,流派等信息的类......并且在创建所有表格后,它将使用此填充数据库.这工作正常.
但是,现在我已经对我的SampleData类进行了一些更改,并且我希望它使用该数据更新数据库(删除已删除的行,插入已添加的新行等等)而不删除数据库.我的数据库管理员不允许我运行DROP命令,因此使用DropCreateDatabaseAlways我的Seed不会起作用.我已在本地完成此操作,它可以正常工作,删除数据库,再次使用表(模型相同)和新的示例数据创建它.
有没有办法这样做,而不必每次我想更新样本数据时删除数据库?也许代码优先迁移(即使我的模型没有改变,只是数据)?
I have a method for updating some tables. For update I need get first of TestProcess, but I don't like that. How can I update TestProcess without select(firstOrDefault) operation, used only for the update operation?
Example of method:
public void UpdateTestProcess(int id, string updateID)
{
using (TestEntities context = new TestEntities())
{
TestProcess pr = context.TestProcess.FirstOrDefault(x => x.MyID == id);
pr.UpdateID = updateID;
context.TestProcess.Attach(pr);
context.ObjectStateManager.ChangeObjectState(pr, EntityState.Modified);
context.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud) 我的理解是不应该在并发Web请求之间共享DbContext实例,所以绝对不能跨线程.但是如何在非并发Web请求中共享它呢?
由于线程敏捷性(ASP.Net中线程敏捷性的含义是什么?),我是否正确,一个线程可以在它死之前处理多个Web请求?
如果是这样,依赖为每个线程注入DbContext实例是否安全?
原因是我使用Unity,它不包括每个请求的生命周期选项.从MVC,EF - DataContext单例实例Per-Web-Request在Unity中,我想我可以使用自定义LifetimeManager; 我只是想知道使用PerThreadLifetimeManager是否安全和充足.
asp.net-mvc entity-framework dependency-injection unity-container dbcontext
有没有办法从DbSet继承?我想添加一些新属性,如下所示:
public class PersonSet : DbSet<Person>
{
public int MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在我的DbContext中实例化它
public partial MyContext : DbContext
{
private PersonSet _personSet;
public PersonSet PersonSet
{
get
{
_personSet = Set<Person>(); // Cast Error here
_personSet.MyProperty = 10;
return _personSet;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我的应用程序越来越大,到目前为止,我有一个单独的MyDbContext应用程序所需的所有表.我希望(有关概述的缘故),以它们分开成多个DbContext,比如MainDbContext,EstateModuleDbContext,AnotherModuleDbContext和UserDbContext.
我不确定这是怎么做的可能因为我现在正在使用dependecy injection(ninject)将我的DbContext放在我的UnitOfWork类上,如:
kernel.Bind(typeof(IUnitOfWork)).To(typeof(UnitOfWork<MyDbContext>));
Run Code Online (Sandbox Code Playgroud)
我应该通过依赖注入和显式设置放弃这种方法,DbContext我希望在我的服务上使用,例如:
private readonly EstateService _estateService;
public HomeController()
{
IUnitOfWork uow = new UnitOfWork<MyDbContext>();
_estateService = new EstateService(uow);
}
Run Code Online (Sandbox Code Playgroud)
代替:
private readonly EstateService _estateService;
public HomeController(IUnitOfWork uow)
{
_estateService = new EstateService(uow);
}
Run Code Online (Sandbox Code Playgroud)
或者这有另一种更好的方法吗?另外作为一个附带问题,我不喜欢传递uow给我的服务 - 还有另一个(更好的)方法吗?
码
我有这个IDbContext和MyDbContext:
public interface IDbContext
{
DbSet<T> Set<T>() where T : class;
DbEntityEntry<T> Entry<T>(T entity) where T : class;
int SaveChanges();
void Dispose();
}
public …Run Code Online (Sandbox Code Playgroud) c# entity-framework unit-of-work repository-pattern dbcontext
的DbContext在EF Code first器具的工作单位和存储库模式为
MSDN网站上说:
DbContext实例表示工作单元和存储库模式的组合,以便它可以用于从数据库进行查询并将更改组合在一起,然后将更改作为一个单元写回到存储中.DbContext在概念上类似于ObjectContext.
这是否意味着使用另一个UoW和Repository抽象(例如IRepository和IUnitOfWor),超过DbContext是错误的?
换句话说,在DbContext上使用另一个抽象层是否会为我们的代码添加任何其他值?
技术独立DAL(我们的域将取决于IRepository和IUnitofWork而不是DbContext)等值
entity-framework data-access-layer repository unit-of-work dbcontext
我有Umbraco 7网站和MVC.
我想对数据库执行一些自定义操作.
据我所知,我应该使用DbContext进行连接.
我已经引用了System.Data.Entity来获取DbContext类.但是,当我尝试使用DbContext时,我收到一个错误说法
The type or namespace name 'DbContext' could not be found (are you missing
a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
在我的模型名称空间中
public class umbracoDbDSN : DbContext
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
你能让我知道我错过了什么吗?
谢谢
sql-server asp.net-mvc database-connection umbraco dbcontext
我对实体框架比较陌生。最近,我发现这种奇怪的行为(在我看来)确实使我感到困惑。
如果将跟踪实体的状态设置为“已删除”,为什么会从dbContext中删除跟踪实体?
例如:
var customers = db.Customers
.Where(customer => customer.FirstName.Contains("John"))
.Take(10)
.ToList();
//Let's iterate through the customers -list
//and remove the 5th customer
int i = 1;
foreach(var customer in customers)
{
if(i == 5)
{
//An exception will be thrown
db.Entry(customer).State = EntityState.Deleted;
}
i++;
}
Run Code Online (Sandbox Code Playgroud)
当第5位客户的状态从更改为时Unchanged,Deleted整个对象将从上下文甚至是customers-list中销毁。更糟糕的是:这也会引发异常,因为此操作更改了我们正在迭代的列表!
有人可以通过调用将更改保存到数据库之前向我解释为什么会发生这种情况db.SaveChanges()吗?
我有一个解决方案,其中我有一个数据项目,其中包含从现有数据库生成的EF6 .edmx文件.我将实体拆分为一个单独的实体项目,并有一个引用它们的存储库项目.
我添加了一个带有一些常用方法的BaseRepository,并希望对它进行单元测试.班级的顶端看起来像这样......
public class BaseRepository<T> : BaseRepositoryInterface<T> where T : class {
private readonly MyEntities _ctx;
private readonly DbSet<T> _dbSet;
public BaseRepository(MyEntities ctx) {
_ctx = ctx;
_dbSet = _ctx.Set<T>();
}
public IEnumerable<T> GetAll() {
return _dbSet;
}
//...
}
Run Code Online (Sandbox Code Playgroud)
按照我在/sf/answers/1475226511/上找到的代码,我尝试了以下内容......
[TestMethod]
public void BaseRepository_GetAll() {
IDbSet<Patient> mockDbSet = Substitute.For<IDbSet<Patient>>();
mockDbSet.Provider.Returns(GetPatients().Provider);
mockDbSet.Expression.Returns(GetPatients().Expression);
mockDbSet.ElementType.Returns(GetPatients().ElementType);
mockDbSet.GetEnumerator().Returns(GetPatients().GetEnumerator());
MyEntities mockContext = Substitute.For<MyEntities>();
mockContext.Patients.Returns(mockDbSet);
BaseRepositoryInterface<Patient> patientsRepository
= new BaseRepository<Patient>(mockContext);
List<Patient> patients = patientsRepository.GetAll().ToList();
Assert.AreEqual(GetPatients().Count(), patients.Count);
}
private IQueryable<Patient> GetPatients() {
return new …Run Code Online (Sandbox Code Playgroud) 嘿,我刚刚学会了如何使用扩展方法,并非常兴奋地在我当前的项目中实现它.
我的目标:
我想检查我的表中是否存在辅助类中的条目,因为我将在多个控制器中使用它以确定在我的导航栏中显示哪些导航链接:
我的问题: 我不知道如何在我的静态助手类中访问我的dbcontext.我的dbcontext控制器接受一个参数,我不知道如何传递我的静态类.我认为创建一个新的dbcontext将解决我在下面解释的范围问题,但我没有看到我如何将option参数传递给我的构造函数.
我尝试了什么: 将ApplicationDbContext作为参数传递.这适用于我的控制器中的单个方法调用,但是当调用多个扩展方法时(为了检查用户具有哪些游戏帐户),我得到一个ObjectDisposedException.
ObjectDisposedException:无法访问已处置的对象.此错误的常见原因是处理从依赖项注入解析的上下文,然后尝试在应用程序的其他位置使用相同的上下文实例.如果您在上下文中调用Dispose()或将上下文包装在using语句中,则可能会发生这种情况.如果使用依赖项注入,则应该让依赖项注入容器负责处理上下文实例.对象名:'ApplicationDbContext'.
根据我的理解,这是一个范围问题,第一个方法调用在完成时处理上下文,我试图在下一次调用中使用相同的上下文?我该怎么做才能解决这个问题?
我尝试阅读此链接注入DbContext时无法访问ASP.NET Core中的已处置对象,但它对我没有帮助,因为它需要Startup.cs类中的ApplicationBuilder.
解决方案更新 我在每个方法调用之后都放置了dbcontext,因为我将它放入一个变量中.相反,我直接在传递的上下文上调用它,它可以工作:
c# extension-methods entity-framework dbcontext asp.net-core-2.0
dbcontext ×10
c# ×6
asp.net-mvc ×3
unit-of-work ×2
dbset ×1
inheritance ×1
nsubstitute ×1
repository ×1
sql-server ×1
state ×1
umbraco ×1
unit-testing ×1
updates ×1