数据库设计的实体框架中的导航属性

时间:2015-04-30 05:08:30

标签: c# mysql entity-framework entity-framework-5

我正在尝试为库存管理系统设计数据库。这是我在代码优先的数据模型设计。但我觉得桌面设计存在一些问题。

Supplier Table

public class Supplier
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SupplierId { get; set; }

    [Index, MaxLength(50),Display(Name="Company Name"),Required]
    public string ComapnyName { get; set; }

    [Index, MaxLength(50),Display(Name="Contact Person"),Required]
    public string ContactPerson { get; set; }

    [Column(TypeName = "ntext"),Required]
    public string Address { get; set; }

    [Required,Display(Name="Mobile Number")]
    public string MobileNumber { get; set; }

    [Display(Name="Land Line Number")]
    public string LandLineNumber { get; set; }

    [DataType(DataType.EmailAddress),Display(Name="Email")]
    public string Email { get; set; }

    [HiddenInput]
    public DateTime? Date_From { get; set; }

    [HiddenInput]
    public DateTime? Date_To { get; set; }

    [Required]
    public bool Active { get; set; }

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

    public virtual ICollection<Category> Categories { get; set; }
}

Category Table

public class Category
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CategoryId { get; set; }

    [Required,Index,MaxLength(50),Display(Name="Category Name")]
    public string CategoryName { get; set; }

    [Column(TypeName = "ntext"),Display(Name="Category Description")]
    public string CategoryDesc { get; set; }

    [HiddenInput]
    public DateTime? Date_from { get; set; }

    [HiddenInput]
    public DateTime? Date_to { get; set; }

    [Required]
    public bool Active { get; set; }

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

    public virtual ICollection<Supplier> Suppliers { get; set; }
}

这里需要的是:[在类别表中]单个类别可以有多个产品,对于单个类别,可以有多个供应商。 [在供应商表格中]单个供应商可以提供多种产品,也可以提供多个类别。

但是我的桌面设计不利于这种情况。怎么纠正这个?任何帮助将不胜感激。

注意:如果您需要任何其他信息,请告诉我。

Product

public class Product
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProductId { get; set; } //Uniquely identifies product

    [Required, Index(IsUnique = true), MaxLength(50), Display(Name = "Product Name")]
    public string ProductName { get; set; } //Product Name

    [Required, Column(TypeName = "ntext"), Display(Name = "Product Description")]
    public string ProductDesc { get; set; } //Product Description

    [Required,Display(Name="Select Warehouse location")]
    public int WarehouseId { get; set; }

    [ForeignKey("WarehouseId")]
    public virtual Warehouse warehouse { get; set; }

    [Required,Display(Name="Category of the Product")]
    public int CategoryId { get; set; } //Identifies product category

    [ForeignKey("CategoryId")]
    public virtual Category category { get; set; }

    [Required,Display(Name="Select Supplier of this product")]
    public int SupplierId { get; set; } //Identifies product supplier

    [ForeignKey("SupplierId")]
    public virtual Supplier supplier { get; set; }

    [Required, Display(Name = "Quantity Per Unit")]
    public int QuantityPerUnit { get; set; } //Product quantity per unit

    [Required, Display(Name = "Unit Cost Price")]
    public decimal UnitCostPrice { get; set; } // Product unit price

    [Required, Display(Name = "Unit Selling Price")]
    public decimal UnitSellingPrice { get; set; } // Unit Selling price

    [Required, Display(Name = "Unit Weight in KG")]
    public int UnitWeight { get; set; } // Product unit weight

    [Display(Name = "Unit Size")]
    public string UnitSize { get; set; } // Product unit size, S, M, L

    [Display(Name = "Any discount on this product")]
    public decimal Discount { get; set; } // Discount offered by supplier

    [Required, Display(Name = "Unit in Stock")]
    public int UnitInStock { get; set; } //Product units in stock

    [Display(Name = "Unit in Order")]
    public int UnitInOrder { get; set; } // units in order from supplier

    [Display(Name = "Reorder Level")]
    public int ReOrderLevel { get; set; } // Product margin for re-ordering

    [Display(Name = "Note")]
    public string Note { get; set; } // Some note for product

    public bool Active { get; set; } //to mention if this product is being active/dis-continued by the company

    [HiddenInput]
    public DateTime? date_from { get; set; } //date when this product was added

    [HiddenInput]
    public DateTime? date_to { get; set; } //date when this product was discontinued by company
}

以上是产品类型号。我认为从产品类型本身我可以提取哪个产品是由哪个供应商提供的,每个供应商提供的每个产品的类别?

2 个答案:

答案 0 :(得分:0)

您似乎正在尝试混合参考数据,例如&#34;产品&#34;,他们的&#34;类别&#34;和&#34;供应商&#34;提供的运营数据,例如&#34;产品,提供&#34;。

我以这种方式重新设计您的实体(和数据库表)(省略属性和标量属性):

public class Category
{
    // Scalar properties here

    // This navigation property describes "categories-products" hierarchy
    public virtual ICollection<Product> Products { get; set; }
}

public class Supplier
{
    // Scalar properties here
}

public class Product
{
    // Scalar properties here

    // This navigation property describes "categories-products" hierarchy
    public int CategoryId { get; set; }         
    public virtual Category Category { get; set; }
}

// This entity describes products, that were supplied by any supplier.
// Note, that we don't need category here, because it can be received through Product.Category reference
public class SuppliedProduct
{
    public int Id { get; set; }
    // the date and time, when product was supplied
    public DateTime WhenSupplied { get; set; }
    // the quantity for supplied product
    public decimal Quantity { get; set; }
    // who supplied this product
    public int SupplierId { get; set; }
    public virtual Supplier Supplier { get; set; }
    // what product was supplied
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

答案 1 :(得分:0)

我发现在初始设置数据库时很难从代码优先方法开始。

其中一个EF忍者(Julie Lerman)推荐的一种方法是,首先使用数据库第一种方法,然后再切换到代码(使用迁移)。

如果使用PK和FK正确设置数据库,则在Visual Studio中使用DbContext生成器时,您也可以使用正确的域类。