Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC |编辑特定列或向数据库添加新条目无效_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# ASP.NET MVC |编辑特定列或向数据库添加新条目无效

C# ASP.NET MVC |编辑特定列或向数据库添加新条目无效,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我在听这个教程 遗憾的是,没有关于如何添加新部门的演示 我遵循了那个教程,做了一些修改,比如名字等 但现在我被绊倒了,因为我想做的事不可能实现 受到质疑 我在projectgem.Domain中有两个实体 C类 Product.cs 使用接口数据源 namespace Gem.Domain { public interface IStoreDataSource { IQueryable<Product> Products { get; }

我在听这个教程

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

我遵循了那个教程,做了一些修改,比如名字等

但现在我被绊倒了,因为我想做的事不可能实现

受到质疑

我在projectgem.Domain中有两个实体

C类 Product.cs 使用接口数据源

namespace Gem.Domain
{
    public interface IStoreDataSource
    {
        IQueryable<Product> Products { get; }
        IQueryable<Category> Categories { get; }
        void safe();
    }
}
CategoriesController中的构造函数

public class CategoriesController : Controller
{
    private IStoreDataSource _db;   

    public CategoriesController(IStoreDataSource db)
    {
        _db = db;
    }
解决这个问题的一种方法是,我想删除依赖项解析,并使DbStore成为直接对象,但这似乎是不对的


如何操作???

如果您使用的是实体框架,常规例程通常如下所示:

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

最后,我不得不在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;
}

我不确定这是不是最好的方法,但如果有其他更好的方法来实现这一点。请解释一下,因为我是.NET新手,正在学习MVC,它可能会帮助我更多地了解实体框架和MVC。

如果您打算使用。添加您需要传入的参数。在您的情况下_db.Categories.AddupdatedCategory;然后再做_db.SaveChanges;在返回json之前。停止在控制器中进行数据访问。我想添加类别而不是产品。在本教程中,将解释如何将数据添加到子表中,而不是关于父表。父部门是部门,子部门是员工,一个部门可以容纳多个员工。@SizzlingCode这只是一个例子。确保你完成了每一步:1。创建上下文,2。将新实体添加到上下文中,3。当我尝试使用_db.Categories.add添加类别时,请保存我的案例中的更改。您可以在我的问题的图片中看到错误。@SizzlingCode尝试使用IDbSet或DbSet代替。先生,如果您看到我的问题,我已经说过,如果我直接使用db上下文(在我的案例中是StoreDb),我可能可以添加新的类别。但是我在我的应用程序中安装了依赖解析,因为我遵循了那个教程,它叫做structuremap.mvc5,正如你所看到的,我已经有了私有属性_db,我的控制器不需要knw dbcontext类,因为它是松散耦合的,我相信这是他在tut中说的。
public interface IMyDbContext
{
    DbSet<Product> Products { get; set; }
    void SaveChanges();
}
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;
}
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;
    }
}
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);
}