The implementation of the interface calls the TPT, where, since the inheritance from the class calls the TPH

I want to create a self-binding name table and a corresponding file table

As shown by MyFolder1 and MyFile1.

enter image description here

I understand how to get this structure, inheriting from the interface.

Can this be achieved by inheriting from an abstract class?

public class Model1Context : DbContext { public Model1Context() : base("name=Model1") { Database.SetInitializer(new Model1Initializer()); } public virtual DbSet<MyFolder1> MyFolder1s { get; set; } public virtual DbSet<MyFile1> MyFile1s { get; set; } public virtual DbSet<MyFolder2> MyFolder2s { get; set; } public virtual DbSet<MyFile2> MyFile2s { get; set; } } public class Model1Initializer : DropCreateDatabaseIfModelChanges<Model1Context> { } public class MyFolder1 : BasicBO, ISequencedTreeNode { public MyFolder1 MyFolder { get; set; } public int SeqNo { get; set; } [NotMapped] public ISequencedTreeNode Parent { get { return MyFolder; } set { MyFolder = (MyFolder1)value; } } } public class MyFile1 : BasicBO, ISequencedTreeNode { public MyFolder1 MyFolder { get; set; } public int SeqNo { get; set; } [NotMapped] public ISequencedTreeNode Parent { get { return MyFolder; } set { MyFolder = (MyFolder1)value; } } } public interface ISequencedTreeNode { [Required] int SeqNo { get; set; } ISequencedTreeNode Parent { get; set; } } public abstract class SequencedTreeNode : BasicBO { [Required] public int SeqNo { get; set; } public SequencedTreeNode Parent { get; set; } } // [Table("MyFolder2")] public class MyFolder2 : SequencedTreeNode { } // [Table("MyFile2")] public class MyFile2 : SequencedTreeNode { } public class BasicBO { public int Id { get; set; } public string Name { get; set; } } 

Marking BasicBO as abstract does not affect table design.

[Update] I can force TPT to add the [Table ('tablename')] attribute to MyFile2 and MyFolder2, however, these tables will have one Id field and the SequencedTreeNodes table will still be created.

It makes sense to me that an interface can be more efficient at forcing TPT, since properties must always be implemented for an interface. However, I would think that this would happen if I made all the properties inside an abstract SequencedTreeNode. However, if I do this, I get a runtime error. "SeqNo" property is not a declared property in type "MyFile2"

+6
source share

Source: https://habr.com/ru/post/989974/


All Articles