2015-04-30 73 views
1

我试图为库存管理系统设计一个数据库。这是我的代码优先的数据模型设计。但是我觉得在餐桌设计中有一些问题。数据库设计的实体框架中的导航属性

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 
} 

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

+1

我宁愿设计这个问题数据库优先。比代码优先更容易 –

+0

产品是否属于单一类别?供应商是否以某种方式限制了产品/类别? – Dennis

+0

@ Dennis是的,一个产品属于一个类别。 – user3132179

回答

0

看起来您正在尝试将参考数据(例如“产品”,“类别”和“供应商”)与操作数据(例如“提供的产品”)混合使用。

我会重新设计你的实体(和数据库表)这样(的属性和简单属性被省略):

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; } 
} 
+0

我已经更新了这个问题。你可以给你一些想法吗? – user3132179

0

我发现它很难从代码第一种方法开始时最初设置的数据库。

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

如果您使用PK和FK正确设置了数据库,那么如果您在Visual Studio中使用DbContext生成器,那么您将拥有正确的域类。