2

I have Entity structure like below

public abstract class Entity
{
    public int Id { get; set; }
}

public class User : Entity
{
    public ICollection<Product> Products { get; set; }
}

public class Warehouse : Entity
{
    public ICollection<Product> Products { get; set; }
}

public class Product : Entity
{
    public Warehouse Warehouse { get; set; }

    public User User { get; set; }
}

As you can see User can has products and Warehouse also can have products. So Entity framework put 2 foreign keys over Product table that can be nullable.

We could also achieve similiar structure by bit of different entity modelling like below

 public class User : Entity
 {
    public ICollection<UserProduct> Products { get; set; }
 }

public class Warehouse : Entity
{
    public ICollection<WarehouseProduct> Products { get; set; }
}

public class Product : Entity
{

}

public class WarehouseProduct : Entity
{
    public Product Product { get; set; }
    public Warehouse Warehouse { get; set; }
}

public class UserProduct : Entity
{
    public Product Product { get; set; }
    public User user { get; set; }
}

First Design look simpler without introduce new entitties but not sure that it is better or not.

I am trying to find which is best or which circumtances makes one of it better than other.

4

1 回答 1

1

继承也是可能的(EF/CodeFirst):

public abstract class Entity
{
    public int Id { get; set; }
}

public class Product : Entity
{

}

public class Warehouse : Product
{ 
    /* all product fields are available */   
}

public class User : Product
{
    /* all product fields are available */
}

在我看来,这更干燥=>“CodeFirst 视图”。

关于继承的好帖子:http: //goo.gl/1igQ3

于 2012-06-17T15:00:57.810 回答