如何最好地为以下SQL Schema设计实体数据类?

时间:2009-04-01 09:49:11

标签: nhibernate fluent-nhibernate nhibernate-mapping

我有以下数据库架构:

http://lh4.ggpht.com/_SDci0Pf3tzU/SdM3XnAmmxI/AAAAAAAAEps/Ie3xW3ZVNfQ/styleerror.png

问题是如何在Nhibernate中创建实体数据类?

这样做会更好:

public class Store
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual IList<Product> Products { get; set; }
    public virtual IList<Employee> Staff { get; set; }

}

public class Employee
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual Store Store { get; set; }
} 

public class Product
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual double Price { get; set; }
    public virtual IList<Store> StoresStockedIn { get; private set; }
}

或者这更好吗?     公共类商店     {         public virtual int Id {get;私人集; }         public virtual string Name {get;组; }

}

public class Employee
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual Store Store { get; set; }
} 

public class Product
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual double Price { get; set; }

}

public class StoreProduct
{
   public virtual List<Product> Products{get;set;}
   public virtual List<Store> Stores{get;set;};
}

我认为第一个比第二个更容易理解,不是吗?

1 个答案:

答案 0 :(得分:1)

如果您修改'StoreProduct'表,以便它没有代理主键,但是存在2个外键列(ProductId&amp; StoreId)的主键,那么您可以简单地将自己限制为3个实体: - 员工 - 产品 - 商店

您的商店类可以拥有一组产品,可以简单地将其映射为多对多关系。

public class Store
{
   public int Id {get;set;}
   public string Name {get;set;}

   public ISet<Product> Products = new HashedSet<Product>();
}

在Store.hbm.xml中映射:

<set name="Products" table="StoreProducts">
   <key column="Store_Id" />
   <many-to-many class="Product" column="Product_Id" />
</set>

所以,回答你的问题:第一个选项更好。