Asp.net mvc 使用DataContext文件和webConfig文件将Web应用程序与localdb连接时出现问题
我已经使用存储库和DI方法创建了MVCWeb应用程序。我也使用了代码优先的方法 这是我的DataContext文件:Asp.net mvc 使用DataContext文件和webConfig文件将Web应用程序与localdb连接时出现问题,asp.net-mvc,repository-pattern,Asp.net Mvc,Repository Pattern,我已经使用存储库和DI方法创建了MVCWeb应用程序。我也使用了代码优先的方法 这是我的DataContext文件: namespace EfRepPatTest.Data { public class DataContext : DbContext, IDbContext { public new IDbSet<TEntity> Set<TEntity>() where TEntity: class {
namespace EfRepPatTest.Data
{
public class DataContext : DbContext, IDbContext
{
public new IDbSet<TEntity> Set<TEntity>() where TEntity: class
{
return base.Set<TEntity>();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType &&
type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
}
}
类别服务:
public class CategoryService : ICategoryService
{
private IRepository<Category> _categoryRepository;
public CategoryService(IRepository<Category> categoryRepository)
{
this._categoryRepository = categoryRepository;
}
public void Insert(Category category)
{
if (category == null)
throw new ArgumentNullException("Category");
_categoryRepository.Insert(category);
}
}
public class RepositoryService<TEntity> : IRepository<TEntity> where TEntity: class
{
private IDbContext _context;
private IDbSet<TEntity> Entities
{
get { return this._context.Set<TEntity>(); }
}
public RepositoryService(IDbContext context)
{
this._context = context;
}
public void Insert(TEntity entity)
{
Entities.Add(entity);
}
}
公共类CategoryService:ICategoryService
{
私人电子储蓄(分类储蓄);;
公共类别服务(IRepository categoryRepository)
{
这._categoryRepository=categoryRepository;
}
公共空白插入(类别)
{
如果(类别==null)
抛出新的异常(“类别”);
_类别报告。插入(类别);
}
}
存储服务:
public class CategoryService : ICategoryService
{
private IRepository<Category> _categoryRepository;
public CategoryService(IRepository<Category> categoryRepository)
{
this._categoryRepository = categoryRepository;
}
public void Insert(Category category)
{
if (category == null)
throw new ArgumentNullException("Category");
_categoryRepository.Insert(category);
}
}
public class RepositoryService<TEntity> : IRepository<TEntity> where TEntity: class
{
private IDbContext _context;
private IDbSet<TEntity> Entities
{
get { return this._context.Set<TEntity>(); }
}
public RepositoryService(IDbContext context)
{
this._context = context;
}
public void Insert(TEntity entity)
{
Entities.Add(entity);
}
}
public class RepositoryService:i位置:类
{
私有上下文_上下文;
私有IDbSet实体
{
获取{返回此。_context.Set();}
}
公共存储服务(IDbContext上下文)
{
这._context=context;
}
公共无效插入(TEntity实体)
{
实体。添加(实体);
}
}
当我第一次运行应用程序时,它将创建本地数据库。但是当我要插入数据时,我没有从应用程序中得到任何错误,它也没有将我的数据插入数据库
这是什么原因?我做错了什么
感谢您的帮助 您应该在
\u context
上调用SaveChanges()
,在所有这样的更改之后,例如:
public void Insert(TEntity entity)
{
Entities.Add(entity);
_context.SaveChanges();
}
在所有类似于以下内容的更改之后,您应该在
\u context
上调用SaveChanges()
:
public void Insert(TEntity entity)
{
Entities.Add(entity);
_context.SaveChanges();
}
显示插入数据的代码更新了问题什么是
categoryService
?@Ankita数据存储在DataContext
中?@teo van kot:我已经更新了所有的东西。显示插入数据的代码更新了问题什么是categoryService
?@Ankita数据存储在DataContext
中?@teo van kot:我已经更新了所有的内容事情。我是否也需要为删除方法执行它?是的,也需要为更改执行它。是的,对于更新方法,我已经执行了。我会添加它删除太多。谢谢你的回答我是否也需要为删除方法执行它?是的,也需要为更改执行它。是的,对于更新方法我已经执行了。我会添加它删除太多。谢谢你的回答