0

这是我的类属性:

    public int ProductId { get; set; }
    public string Name { get; set; }
    public DateTime Creation { get; set; }
    public int BrandId { get; set; }
    public virtual Brand Brand { get; set; }
    public int CreatedBy { get; set; }
    public decimal Price { get; set;         
    public string Description { get; set; }
    public string Properties { get; set; }
    public bool OnFrontPage { get; set; }
    public int StockCount { get; set; }
    public decimal MarketPrice { get; set; }
    public int CurrencyId { get; set; }
    public virtual Currency Currency { get; set; }
    public int TaxRate { get; set; }
    public bool IsNewProduct { get; set; }
    public bool IsActive { get; set; }
    public string MetaDescription { get; set; }
    public string MetaKeyWord { get; set; }
    public int UnitId { get; set; }
    public virtual Unit Unit { get; set; }
    public int PointValue { get; set; }
    public string RelatedProductIds { get; set; }
    public int MaxSaleCount { get; set; }
    public int ProductGroupId { get; set; }
    public virtual ProductGroup ProductGroup { get; set; }
    public virtual List<ProductCategory> ProductCategories { get; set; }
    public virtual List<ProductType> ProductTypes { get; set; }
    public virtual List<ProductImage> ProductImages { get; set; }
    public virtual List<Promotion> Promotions { get; set; }
    public int SaleCount { get; set; }
    public virtual List<ProductChance> ProductChances { get; set; }
    public string ProductCode { get; set; }

这是由上下文创建的 sql 查询。上下文添加 [Category_CategoryId] 列但是我在我的班级或我的数据库表中没有任何关于类别的信息

  SELECT 
[Extent1].[ProductId] AS [ProductId], 
[Extent1].[Name] AS [Name], 
[Extent1].[Creation] AS [Creation], 
[Extent1].[BrandId] AS [BrandId], 
[Extent1].[CreatedBy] AS [CreatedBy], 
[Extent1].[Price] AS [Price], 
[Extent1].[Description] AS [Description], 
[Extent1].[Properties] AS [Properties], 
[Extent1].[OnFrontPage] AS [OnFrontPage], 
[Extent1].[StockCount] AS [StockCount], 
[Extent1].[MarketPrice] AS [MarketPrice], 
[Extent1].[CurrencyId] AS [CurrencyId], 
[Extent1].[TaxRate] AS [TaxRate], 
[Extent1].[IsNewProduct] AS [IsNewProduct], 
[Extent1].[IsActive] AS [IsActive], 
[Extent1].[MetaDescription] AS [MetaDescription], 
[Extent1].[MetaKeyWord] AS [MetaKeyWord], 
[Extent1].[UnitId] AS [UnitId], 
[Extent1].[PointValue] AS [PointValue], 
[Extent1].[RelatedProductIds] AS [RelatedProductIds], 
[Extent1].[MaxSaleCount] AS [MaxSaleCount], 
[Extent1].[ProductGroupId] AS [ProductGroupId], 
[Extent1].[SaleCount] AS [SaleCount], 
[Extent1].[ProductCode] AS [ProductCode], 
[Extent1].[Category_CategoryId] AS [Category_CategoryId]
FROM [dbo].[Product] AS [Extent1]}

类别:

public int CategoryId { get; set; }

public string Name { get; set; }
public string LinkText { get; set; }
public int? ParentId { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public bool IsActive { get; set; }
public virtual List<Product> Products { get; set; }
public DateTime Creation { get; set; }
public int CreatedBy { get; set; }
public bool IsParentCategory { get; set; }
4

1 回答 1

1

好吧,您可能将关系设置为一对多(或者当您拥有另一个实体的 IList 时由 EF 完成,而另一部分没有任何内容,我必须承认我不知道)。

无论如何,这并不是因为您不想在产品类中看到导航属性“类别”,因此数据库不需要 FK 来保持这两个实体之间的关系。

数据库的需求与其对应的对象不同。

其他示例:如果您创建多对多关系

public class A {
public virtual IList<B> Bs {get;set;}
}

public class B {
 public virtual IList<A> As {get;set;}
}

EF 将创建一个表 A、一个表 B 和一个表 AB。但在对象世界中,AB 表不作为实体存在(不需要)。

于 2012-06-22T17:10:13.710 回答