3

In the MSDN they show how to implement EF TPT with this example:

    public abstract class BillingDetail
    {
        public int BillingDetailId { get; set; }
        public string Owner { get; set; }
        public string Number { get; set; }
    }

    [Table("BankAccounts")]
    public class BankAccount : BillingDetail
    {
        public string BankName { get; set; }
        public string Swift { get; set; }
    }

    [Table("CreditCards")]
    public class CreditCard : BillingDetail
    {
        public int CardType { get; set; }
        public string ExpiryMonth { get; set; }
        public string ExpiryYear { get; set; }
    }

    public class InheritanceMappingContext : DbContext
    {
        public DbSet<BillingDetail> BillingDetails { get; set; }
    }

Regarding (*) I have this question:

Can I have a DbSet per child in the hierarchy? ie:

    public class InheritanceMappingContext : DbContext
    {
        public DbSet<BankAccount> BankAccount { get; set; }
        public DbSet<CreditCard> CreditCards{ get; set; }
    }

I'm asking this because I'd like to have a main father class from which all the other classes inherit, ie:

    public abstract class DatabaseItem
    {
        [Key]
        public long DatabaseItemId { get; set; }
    }

    public User : DatabaseItem
    {
        ...
    }

    public Picture : DatabaseItem
    {
        ...
    }

    public Gallery : DatabaseItem
    {
        ...
    }

So that given an ID, I know that it corresponds to only one thing.
In the past in other projects I didnt do this, and used specifical IDs: "UserId", "PictureId", "GalleryId".

In this case, I will be having only one DbSet?

   DbSet<DatabaseItem> DatabaseItems
sports
  • 7,851
  • 14
  • 72
  • 129
  • I'm fuzzy about what you want exactly. You want to have multiple tables, but use the same ID field? Or you want all your entities to use the same table? The reason I ask is that TPT means separate tables, TPH is all in one table. – Erik Funkenbusch Jul 06 '14 at 19:30
  • I want to have id's as Facebook does: given an id, you know it corresponds to a picture or a user or an album. In my previous way of doing things, I needed an id and a type ("picture with id X"). I don't know if using TPT is the best way of achieving this though – sports Jul 06 '14 at 19:34
  • in the facebook API, you can do: GET graph.facebook.com/[node-id] and it will bring you what that is (being a picture, a user, or whatever) – sports Jul 06 '14 at 19:36
  • I really don't care about any of that stuff. You didn't answer the question I asked. How do you want this be structured in the database? Everything in one table, or individual tables for each type? – Erik Funkenbusch Jul 06 '14 at 21:17
  • Sorry, didn't understood your question. I was thinking of a table DatabaseItems that only has one column (DatabaseItemId) and then all the other tables, eg. Users(DatabaseItemId, Name, HashedPassword), Pictures(DatabaseItemId, Filename, ...), etc. – sports Jul 06 '14 at 21:44
  • Now I was reading this article: http://msdn.microsoft.com/en-us/magazine/jj553510.aspx and I'm not sure of achieving what I want this way (by "way" I mean "TPT" and "base class") – sports Jul 06 '14 at 21:47

0 Answers0