Entity Framework: How to avoid Discriminator column from table?

前端 未结 7 1826
无人及你
无人及你 2020-12-08 07:10

I have the following table created using Entity Framework Code First approach.

  1. How do I modify the C# code so that the unwanted Discriminato
相关标签:
7条回答
  • 2020-12-08 07:42

    Since both "GiftCouponPayment" and "ClubCardPayment" derives from "PaymentComponent" EF will not use separate tables and will need that column. If you want a different behaviour you would have to override the default table access and map the fields to your classes (which I think you don't want to do) Not sure if there is an easy way to to this. From entity first, I know that there is a way through the template which creates the tables.
    The same is for the foreign key column name. EF uses that way of creating the name for key/foreignkey names. If you want to format the table as you like, you have to do it all yourself, which leads to the question why use EF then at all.
    Is there a particular reason why you want to do that, other than cosmetics?

    0 讨论(0)
  • 2020-12-08 07:43

    TPH inheritance needs special column which is used to identify the type of entity. By default this column is called Discriminator and contains names of derived entities. You can use Fluent-API to define different column name and different values. You can also use your MyType column directly because it is actually a discriminator but in such case you cannot have that column in your entity (column can be mapped only once and if you use it as discriminator it is already considered as mapping).

    The name of foreign key column can be again controlled with Fluent-API:

    protected override void OnModelCreating(DbModelBuilder modelbuilder)
    {
        modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
        // Example of controlling TPH iheritance:
        modelBuilder.Entity<PaymentComponent>()
                .Map<GiftPaymentComponent>(m => m.Requires("MyType").HasValue("G"))
                .Map<ClubPaymentComponent>(m => m.Requires("MyType").HasValue("C"));
    
        // Example of controlling Foreign key:
        modelBuilder.Entity<Payment>()
                    .HasMany(p => p.PaymentComponents)
                    .WithRequired()
                    .Map(m => m.MapKey("PaymentId"));
    }
    
    0 讨论(0)
  • 2020-12-08 07:43

    Sample code to remove Discriminator column and get column named PaymentId as discriminator instead, therefore solving both your questions. Based on Microsofts Fluent Api original documentation.

    https://msdn.microsoft.com/en-us/library/jj591617%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396

    public enum MyEnum
    { 
        Value1, Value2
    }
    
    public class MyBaseClass
    
    { 
        [NotMapped]
        public MyEnum PaymentId { get; protected set; }
    }
    
    public class DerivedOne: MyBaseClass
    {
        public DerivedOne()
        {
            PaymentId = MyEnum.Value1;
        } 
    }
    
    public class DerivedTwo: MyBaseClass
    {
        public DerivedTwo()
        {
            PaymentId = MyEnum.Value2;
        }
    }
    
    public class MyDbContext : DbContext
    {
        DbSet<MyBaseClass> MyBaseClass { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<MyBaseClass>()
                .Map<DerivedOne>(x => x.Requires("PaymentId").HasValue((int)PaymentId.Value1))
                .Map<DerivedTwo>(x => x.Requires("PaymentId").HasValue((int)PaymentId.Value2));
        }
    }
    
    0 讨论(0)
  • 2020-12-08 07:49

    Add attribute [NotMapped] if the property not going to mapped to column.

    0 讨论(0)
  • 2020-12-08 07:49

    As you're using subclasses, the Discriminator column is required to distinguish between each type of your subclasses.

    0 讨论(0)
  • 2020-12-08 07:53

    In order to avoid Discriminator column from table you just need to add annotation [NotMapped] over your derived class.

    0 讨论(0)
提交回复
热议问题