c# - 更新数据库上下文以包含 View 模型

标签 c# asp.net-mvc entity-framework

我正在将 Entity Framework 与 MVC4 结合使用。我不确定如何将我的 ViewModel 添加到我的 dbcontext。就像在我的 DbContext 中声明它一样简单吗?基本上我希望在我的 Controller 中使用我的 View 模型并将其传递给我的 View 。我在尝试实现此目标时遇到了很多问题。

namespace BagInventory.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class FreshouseSalesEntities : DbContext
    {
        public FreshouseSalesEntities()
            : base("name=FreshouseSalesEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Materials_Packer> Materials_Packer { get; set; }
        public DbSet<Materials_Product> Materials_Product { get; set; }
        public DbSet<Materials_PackerProduct> Materials_PackerProduct { get; set; }
        public DbSet<Materials_Vendor> Materials_Vendor { get; set; }
        public DbSet<Materials_Log> Materials_Log { get; set; }
        //Below is my view model I wish to add to dbcontext
        public DbSet<BigViewModel> BigViewModel{get;set;}


    }
}

这是正确的做法吗?

最佳答案

想想 ViewModel 中的话。一种 super 简化的查看方式是,ViewModel 是一种在 View 和模型之间来回转换数据的方法。但是,它不应该用作您的域模型。

如果您的 ViewModel 和您的域模型完全相同,那么您不需要 ViewModel。他们并不总是需要的。考虑您的用例,然后做出适当的选择。

关于c# - 更新数据库上下文以包含 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849776/

相关文章:

c# - Razor 语法 PHP 等价物

c# - 获取下一个最小的 Double 数

c# - EF 是否有一个事件系统,您可以在其中修改模型,然后再保存它?

asp.net-mvc - "The Id field is required"创建时的验证消息; ID 未设置为 [必填]

c# - Azure Blob TIFF 到 PNG 转换,无需下载

c# - "Sequence contains more than one element"将数据库数据转换为列表时

entity-framework - 在 EF Code First Orderby 函数期间无法将类型 'System.Int32' 转换为类型“System.Object”

c# - 在 Sound forge 中编写脚本

c# - 从表示层映射到后端的通用方式

c# - 用Flags扩展enum,干扰当前使用