基于没有键定义的类mvc5 [英] Based on class that has no key defined mvc5

查看:64
本文介绍了基于没有键定义的类mvc5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从此控制器添加视图.我只需要此视图即可显示不用于插入,更新或删除的数据

I try to add view from this controller . I only need this view to show data not for insert or update or delete

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:实体类型"New"没有定义键. 定义entityType的键

there was an error running the selected code generator: 'unable to retrieve metadata for 'Musicstore.Model.new' one or more validation error were detected during model generation musicstore,models.New :entity type'New' has no key defined . define the key of entityType

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

    }
}

推荐答案

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

There are two options here. First, if this class is mapped to a table in your database, every model in entity framework requires a primary key. Add this into your model:

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

这将创建一个名为Id的新属性,并且[Key]属性使其成为主键.从技术上讲,您不需要该属性,因为EF会选择Id属性并将其用作键,但是我希望保持明确.

This creates a new property called Id and the [Key] attribute makes it a primary key. Technically you don't need the attribute as EF will pick up Id property and use it as a key, but I prefer to be explicit.

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

Alternatively, if you don't want this class to be a table in your database, add the NotMapped attribute to the class like this:

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

}

这篇关于基于没有键定义的类mvc5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆