簡體   English   中英

實體框架FK錯誤

[英]Entity Framework FK error

我的代碼返回以下錯誤:

無法將屬性“ 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; }
}

必須在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具有一組規則 ,可在發現關系時應用這些規則來定位外鍵屬性。 約定基於屬性的名稱。 如果將外鍵屬性命名為[Target Type Key Name][Target Type Name] + [Target Type Key Name][Navigation Property Name] + [Target Type Key Name]則會按慣例發現外鍵屬性。 如果您使用ForeignKey數據注釋,則這些規則將被忽略,因為您正在向EF明確告知要使用的FK屬性。

CartID的類型不是必須是整數而不是字符串嗎? 我不確定這是否可以解決您的問題,但似乎這就是錯誤消息所要討論的內容。

嘗試遵循EF代碼約定,所以不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代碼約定。 請參閱本文以獲取更多信息: 代碼優先約定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM