【问题标题】:EntityType: EntitySet 'Orderlines' is based on type 'Orderline' that has no keys definedEntityType:EntitySet 'Orderlines' 基于没有定义键的类型 'Orderline'
【发布时间】:2014-04-21 18:06:20
【问题描述】:

我正在尝试从控制器访问“Pizza”模型,但出现以下错误 - (在尝试添加 OrderLine 模型之前可以正常工作):

EntityType 'Orderline' 没有定义键。定义此 EntityType 的键。 Orderlines: EntityType: EntitySet 'Orderlines' 是基于 键入没有定义键的“Orderline”。

我一直看到问题是添加 [Key],但我已经这样做了。

披萨:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.Domain.Entities
{
    public class Pizza
    {
    [Key]
    public int PizzaId { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Size { get; set; }
    public string Status { get; set; }
    }
}

订单线

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PizzaShop1.Domain.Entities
{
public class Orderline
{
    [Key]
    public int OrderlineId;

    [ForeignKey("PizzaId")]
    public Pizza PizzaId;
    public decimal OrderlinePrice;
}
}

DbContext

namespace Pizza.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Pizza> Pizzas { get; set; }
        public DbSet<Orderline> Orderlines { get; set; }
    }
}

【问题讨论】:

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


    【解决方案1】:

    如果我没记错的话,它一定是一个属性。

    public class Orderline
    {
        [Key]
        public int OrderlineId {get; set;}
    
        [ForeignKey("PizzaId")]
        public Pizza PizzaId {get; set;}
        public decimal OrderlinePrice {get; set;}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2021-03-26
      相关资源
      最近更新 更多