【问题标题】:Based on class that has no key defined mvc5基于没有键定义 mvc5 的类
【发布时间】:2017-06-01 15:25:02
【问题描述】:

我尝试从这个控制器添加视图。我只需要这个视图来显示数据,而不是插入、更新或删除

public ActionResult Index()
{
    var CartObj = ShoppingCart.GetCart(this.HttpContext);

    var classshop = new New
    {
        CartItems = CartObj.GetCartItems(),
        CartTotal = CartObj.GetSum()
    };

    return View(classshop);
}

namespace MusicStore.Models
{
    public class ShoppingCart
    {
        MusicStoreEntities dbo = new MusicStoreEntities();
        string ShoppingCartID { get; set; }

        public const string CartSessionKey = "CartId";
        public static ShoppingCart GetCart(HttpContextBase Context)
        {
            var cart = new ShoppingCart();
            cart.ShoppingCartID = cart.GetCardId(Context);
            return cart;
        }
        public static ShoppingCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public  List<Cart> GetCartItems()
        {
            return dbo.Carts.Where(a => a.CartId == ShoppingCartID).ToList();
        }

        public decimal? GetSum()
        {
            decimal? Sum = (from items in dbo.Carts
                         where items.CartId == ShoppingCartID
                         select (int)items.Count * items.album.Price).Sum();
            return Sum ?? decimal.Zero;
        }
    }
}

然后我得到了这个错误:

运行所选代码生成器时出错: '无法检索 'Musicstore.Model.new' 的元数据 在模型生成期间检测到一个或多个验证错误 musicstore,models.New :entity type'New' 没有定义键。 定义entityType的key

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MusicStore.Models
{
    public class New
    {
        public List<Cart> CartItems { get; set; }
        public decimal? CartTotal { get; set; }

    }
}

【问题讨论】:

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


    【解决方案1】:

    这里有两个选项。首先,如果这个类映射到数据库中的表,实体框架中的每个模型都需要一个主键。将此添加到您的模型中:

    [Key]
    public int Id { get; set; }
    

    这将创建一个名为Id 的新属性,[Key] 属性使其成为主键。从技术上讲,您不需要该属性,因为 EF 将获取 Id 属性并将其用作键,但我更喜欢明确。

    或者,如果您不希望此类成为数据库中的表,请将 NotMapped 属性添加到类中,如下所示:

    [NotMapped]
    public class New
    {
        public List<Cart> CartItems { get; set; }
        public decimal? CartTotal { get; set; }
    
    }
    

    【讨论】:

    • 但我不会插入或更新或删除。只是为了查看其他表中的数据
    • 所以它没有映射到你数据库中的表?
    • 我在 mv4 中使用了相同的代码。它正在工作,但在 mvc5 中需要密钥。
    • 检查类型是否已通过使用忽略方法或未映射的属性数据注释明确排除
    • 您是否也在下拉菜单中选择上下文?
    【解决方案2】:

    我知道这是旧的,但我刚刚遇到了这个问题。

    当我在 Models 文件夹中创建一个类 CreateEmployeeViewModel 时会发生什么,Visual Studio “聪明地”在我的 DB Context 类中放了一行

    public System.Data.Entity.DbSet<eManager.Web.Models.CreateEmployeeViewModel>
           CreateEmployeeViewModels { get; set; }
    

    因此在下一个update-migration 上创建了一个表。删除此行删除了对关键字段的要求。

    注意:如果表已创建,您可能还需要将行 AutomaticMigrationDataLossAllowed = true; 添加到您的 DBMigrationConfiguration 类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多