0

这里有什么问题?我在产品和交易之间有很多对多。尝试构建view但我无法获得任何智能感知来加载ProductTransaction表的查询。

实体 1

     [Table("Transactions")]
public class Transaction
{
        [Key]
        public virtual int TID { get; set; }

        public virtual int FromUserID { get; set; }
        public virtual int ToUserId { get; set; }
        public virtual int itemForId { get; set; }

        public virtual ICollection<Product> Products { get; set; }

}

实体 2

      [Table("Products")]
public class Product
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public virtual int ProductId { get; set; }

    [Required]
    [StringLength(50, ErrorMessage="Name can't be longer than 50 characters bitch")]
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }

    public virtual DateTime? UploadDate { get; set; }

    public virtual byte[] ProductImage { get; set; }
    [StringLength(25)]
    public virtual string MimeType { get; set; }

    public virtual int Views { get; set; }

    public virtual int Qty { get; set; }

    // Relations
    public virtual UserProfile User { get; set; }
    public virtual int UserId { get; set; }
    public virtual int CategoryId { get; set; }

    public virtual ICollection<Bag> Bags { get; set; }
    public virtual ICollection<Transaction> Transactions{ get; set; }
}

查看所有交易和交易的控制器方法:查询没有智能感知Products

     public ViewResult GetTrades()
    {
        //Include is not recognizing any nav properties

        var friend = db.Transactions.Include(a => a.Products);

    }

多对多关系

       modelBuilder.Entity<Transaction>()
            .HasMany(p => p.Products)
            .WithMany(t => t.Transactions)
            .Map(m =>
                {
                    m.ToTable("ProductTransactions");
                    m.MapLeftKey("ProductId");
                    m.MapRightKey("TID");


                });
4

1 回答 1

0

DbExtensions.Include(this IQueryable, Expression<Func<T, TProp>>)是一种扩展方法。

您需要包含命名空间才能将其作为扩展方法访问。

using System.Data.Entity;
于 2013-10-05T07:07:02.050 回答