使用DataContext文件和webConfig文件将Web App与localdb连接时出现问题

安基塔

我已经使用存储库和DI方法创建了MVC Web应用程序。我也使用了代码优先方法。

这是我的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"
/>

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

这是我的发布方法:

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

CategoryService:

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

}

RepositoryService:

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

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

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

任何帮助表示赞赏!

特奥·范·科特

你应该叫SaveChanges()_context像这样为前所有变更后:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

连接端口和订阅时出现问题

来自分类Dev

连接端口和订阅时出现问题

来自分类Dev

连接到 mysql wamp 和 django 时出现问题?

来自分类Dev

将Snowflake连接到DataFactory时出现问题

来自分类Dev

使用ring / compojure连接Clojure nREPL时出现问题

来自分类Dev

使用C#连接到Oracle时出现问题

来自分类Dev

使用方法建立连接时出现问题

来自分类Dev

使用ring / compojure连接Clojure nREPL时出现问题

来自分类Dev

将Django App移至生产环境时出现问题

来自分类Dev

用动态路径将R文件连接到python时出现问题。

来自分类Dev

PHP MVC文件连接出现问题

来自分类Dev

发布组织文件时出现问题

来自分类Dev

Python:读写文件时出现问题

来自分类Dev

从.cpp文件读取时出现问题

来自分类Dev

编译Java文件时出现问题

来自分类Dev

读取csv文件时出现问题

来自分类Dev

解析json文件时出现问题

来自分类Dev

运行jar文件时出现问题

来自分类Dev

上载图片文件时出现问题

来自分类Dev

读取.txt文件时出现问题

来自分类Dev

使用python和selenium连接到phantomJs Webdriver时出现问题

来自分类Dev

使用Firebase函数和Express连接到socket.io时出现问题

来自分类Dev

使用MTP将Android ICS连接到Ubuntu时出现问题

来自分类Dev

从Web API服务下载pdf文件时出现问题

来自分类Dev

从Web API服务下载pdf文件时出现问题

来自分类Dev

导入证书和配置文件时出现问题

来自分类Dev

将文件发送到API时出现问题

来自分类Dev

将输入与文件信息进行比较时出现问题

来自分类Dev

将输出写入文件时出现问题