使用 DbContext 文件和 Web 配置文件将 Web 应用程序与 localdb 连接时出现问题

Issue in Connecting Web App with localdb using DataContext file and webConfig file

我已经使用存储库和 DI 方法创建了 MVC Web 应用程序。我也使用过 Code First 方法。

这是我的 DataContext 文件:

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); 
        }

    }
}

我在 Web.Config 文件中定义了连接字符串,如下所示:

<add name="DataContext"
   connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\eCommerce.mdf;Integrated Security=True"
   providerName="System.Data.SqlClient"
/>

请注意,我在这里提到了连接字符串和我的上下文文件的相同名称。

这是我的 post 方法:

  [HttpPost]
        public ActionResult Create(CategoryModel model)//FormCollection collection
        {
            try
            {
                // TODO: Add insert logic here
                if (model == null)
                    return View(model);

                var category = new Category();
                category.Name = model.Name;

                categoryService.Insert(category);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

类别服务:

 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);
        }
}

当我 运行 第一次申请时,它会创建本地数据库。但是当我要插入数据时,我没有从应用程序中得到任何错误,它也没有将我的数据插入到数据库中。

这是什么原因造成的?我在这里做错了什么?

感谢任何帮助!

您应该在 _context 上调用 SaveChanges() 之后,例如:

public void Insert(TEntity entity)
{
    Entities.Add(entity);
    _context.SaveChanges();
}