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.

1 Answer 1

1

Inheritance would also be possible (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 */
}

this is more DRY in my point of view => "CodeFirst view".

Here is a good post about Inheritance

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.