ASP.NET MVC |编辑特定列或向DB添加新条目不起作用

时间:2015-09-02 17:57:55

标签: c# asp.net asp.net-mvc

我跟随这个tuturial ..

http://www.pluralsight.com/courses/mvc4

遗憾的是,没有关于如何添加新部门的演示。

我正在关注那个教程并且做了一些我的更改,就像名字等一样。

但现在因为我想做的事情而无法接受。

即将开始提问

Project Gem.Domain中有两个实体

  1. Category.cs
  2. Product.cs
  3. with interface datasource。

    namespace Gem.Domain
    {
        public interface IStoreDataSource
        {
            IQueryable<Product> Products { get; }
            IQueryable<Category> Categories { get; }
            void safe();
        }
    }
    

    现在,这是在这个Gem.domain被引用的其他项目中。

    将上下文类名称设为StoreDb。

    namespace Gem.Infrastructure
    {
        public class StoreDb : DbContext, IStoreDataSource
        {
            public StoreDb() : base("GemStoreConnection")
            {
    
            }
    
            public DbSet<Product> Products { get; set; }
            public DbSet<Category> Categories { get; set; }
    
            void IStoreDataSource.safe()
            {
                SaveChanges();
            }
    
            IQueryable<Product> IStoreDataSource.Products
            {
                get
                {
                    return Products;
                }
            }
    
            IQueryable<Category> IStoreDataSource.Categories
            {
                get
                {
                    return Categories;
                }
            }
        }
    }
    

    使用structuremap.mvc,与教程中解释的相关性解析相同。

    但是当我尝试从控制器添加新类别时,我无法添加。

    它既不理解.Add()也不理解.Attach方法..

    我的控制器方法更新类别名称。

        [HttpPost]
        public ActionResult UpdateCategoryName_DT(Category category)
        {
            if (!ModelState.IsValid) return Content("Not Valid");
            var updatedCategory = new Category()
            {
                CategoryID = category.CategoryID,
                Name = category.Name
    
            };
    
    //For test using .Add() but not working neither do .Attach
            _db.Categories.Add();
    
    
    
            return Json(updatedCategory);
        }
    

    enter image description here

    CategoriesController中的构造函数

    public class CategoriesController : Controller
    {
        private IStoreDataSource _db;   
    
        public CategoriesController(IStoreDataSource db)
        {
            _db = db;
        }
    

    要解决的一种方法是我想删除依赖项解析并做DbStore的直接对象,但这看起来不对..

    怎么做??

2 个答案:

答案 0 :(得分:0)

如果您使用的是Entity Framework,那么常规例程通常如下所示:

using (var context = new MyDbContext())
{
    var product = new Product("Surface", "Pro");
    context.Products.Add(product);
    context.SaveChanges();
}

示例数据库上下文:

public class MyDbContext : DbContext
{
    DbSet<Product> Products { get; set; }
}

如果你想为你的上下文使用接口,你正在寻找这样的东西:

public interface IMyDbContext
{
    DbSet<Product> Products { get; set; }
    void SaveChanges();
}

答案 1 :(得分:0)

终于结束了,我手动必须在DataSource接口文件中定义Add,Delete Attach方法。

所以现在它看起来像

public interface IStoreDataSource
{
    IQueryable<Product> Products { get;}
    IQueryable<Category> Categories { get;}
    void safe();

    T Attach<T>(T entity) where T : class;
    T Add<T>(T entity) where T : class;
    T Delete<T>(T entity) where T : class;
}

并在StoreDB中添加了这些方法(通常称为db上下文)

public class StoreDb : DbContext, IStoreDataSource
{
    public StoreDb() : base("GemStoreConnection")
    {

    }

    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }

    void IStoreDataSource.safe()
    {
        SaveChanges();
    }

    IQueryable<Product> IStoreDataSource.Products
    {
        get
        {
            return Products;
        }
    }

    IQueryable<Category> IStoreDataSource.Categories
    {
        get
        {
            return Categories;
        }
    }

    T IStoreDataSource.Add<T>(T entity)
    {
        return Set<T>().Add(entity);
    }

    T IStoreDataSource.Delete<T>(T entity)
    {
        return Set<T>().Remove(entity);
    }

    T IStoreDataSource.Attach<T>(T entity)
    {
        var entry = Entry(entity);
        entry.State = EntityState.Modified;
        return entity;
    }
}

最后在Controller中使用Update方法。

public ActionResult UpdateCategoryName_DT(Category category)
{
    if (!ModelState.IsValid) return Content("Not Valid");
    var updatedCategory = new Category()
    {
        CategoryID = category.CategoryID,
        Name = category.Name

    };

    _db.Attach(category);
    _db.safe();


    return Json(updatedCategory);
}

我不确定它是最好的方法,但是如果还有其他更好的方法可以实现这一点。请解释由于我是.NET新手并学习MVC,它可以帮助我更多地了解实体框架和MVC。