实体框架FK错误

时间:2015-04-15 17:13:05

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

我的代码返回以下错误:

  

该物业' cartID'无法配置为导航属性。   该属性必须是有效的实体类型,属性应该具有   一个非抽象的getter和setter。对于集合属性的类型   必须实现ICollection,其中T是有效的实体类型。

我的模型如下:

[Table("ShoppingCarts")]
public class ShoppingCart 
{
    [Key]
    public string cartID { get; set; }

    public virtual ICollection<ShoppingCartItem> CartItems { get; set; }
    public DateTime? DateCreated { get; set; }
    public Guid UserID { get; set; }
}


[Table("ShoppingCartItems")]
public class ShoppingCartItem
{


    private string cartDisplayImg;

    [Key]
    [Display(Name = "Cart Item ID#")]
    public int cartItemID { get; set; }

    [Display(Name = "Cart ID")]
    [ForeignKey("cartID")]
    public string cartID { get; set; }
    [Required]
    public string itemTitle { get; set; }

    public int listingID { get; set; }
    public int sellerID { get; set; }
    [Required]
    public string sellerSKU { get; set; }
    [Required]
    public int Quantity { get; set; }
    public string itemType { get; set; }

    public string condition { get; set; }
    [Required]
    public decimal Price { get; set; }

    public string Make { get; set; }
    public string Model { get; set; }
    public string displayImgPath
    {

        get {
           cartDisplayImg = "http://www.example.com/Images/Phones/" + Make + "-" + Model + "-1.jpg";

           return cartDisplayImg;

        }

    }
    public decimal lineTotal
    {
        get {
            decimal cartLineTotal = Price * Quantity;
            return cartLineTotal; 
        }

    }

}

public class ShopingCartContext : DbContext
{

    public ShopingCartContext()
        : base("PHONEOUTLET_DBConnectionString")
    {
        Database.SetInitializer<ShopingCartContext>(new CreateDatabaseIfNotExists<ShopingCartContext>());

    }


    public DbSet<ShoppingCart> ShoppingCart { get; set; }
    public DbSet<ShoppingCartItem> ShoppingCartItems { get; set; }
}

3 个答案:

答案 0 :(得分:6)

必须在FK属性上使用ForeignKey数据注释以及告诉它哪个导航属性表示它是外键的关系的信息:

[Table("ShoppingCartItems")]
public class ShoppingCartItem
{
   //..
   [Display(Name = "Cart ID")]
   [ForeignKey("Shoppingcart")]
   public string cartID { get; set; }
   public virtual ShoppingCart Shoppingcart { get; set; }
}

或者,您可以将ForeignKey注释应用于导航属性,并告诉它哪个属性是该关系的外键:

[Table("ShoppingCartItems")]
public class ShoppingCartItem
{
   //..
   [Display(Name = "Cart ID")]
   public string cartID { get; set; }

   [ForeignKey("cartID")]
   public virtual ShoppingCart Shoppingcart { get; set; }
}

此外,Code First有一组rules,用于在发现关系时查找外键属性。约定基于属性的名称。如果外键属性名为[Target Type Key Name][Target Type Name] + [Target Type Key Name][Navigation Property Name] + [Target Type Key Name],则会按惯例发现它。如果您使用ForeignKey数据注释,则会忽略这些规则,因为您明确告知EF要使用的FK属性。

答案 1 :(得分:0)

CartID的类型不是必须是整数而不是字符串吗?我不确定这是否会解决您的问题,但似乎这就是错误消息所说的内容。

答案 2 :(得分:0)

尝试遵循EF代码约定,而不是此

    [Display(Name = "Cart ID")]
    [ForeignKey("cartID")]
    public string cartID { get; set; } 

试试这个

    [Display(Name = "Cart ID")]
    public string CartID { get; set; }
    public virtual ShoppingCart Shoppingcart { get; set; }

在类中定义一对多关系的常用方法是在一个类中包含子集合,然后在子类中包含外键属性以及导航属性。


编辑1: 尝试遵循EF代码约定。有关详细信息,请参阅此文章:Code First Conventions