none
Override virtual properties of base class in EF Code First RRS feed

  • Question

  • Hello,

    How do I override a virtual property of a base class? example:

    public BaseClass

    {

    public virtual RowGuid{get; set;}

    }

    and in its config file:

    this.Property(m => m.RowGuid).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);


    Now I want the derived class to override this setting, and have 

    DatabaseGeneratedOption.None

    I tried several things including marking derived property as 'new'.

    Thanks

    • Edited by cp.net Monday, October 15, 2012 2:44 AM
    Monday, October 15, 2012 2:42 AM

Answers

  • Hi Allen,

    I am trying to create tables for Audit Trail to log updates to entity records. So each Audit class will be derived from main entity class to get all the fields, example Order_Audit derived from Order. So, if Order class has identity fields like 'AddedDate' and 'RowGuid', I do not want them as identity in the corresponding Order_Audit class since they will need to be updated with values from main entity table.

    So, right now I have given up on TPC because as you said, it is not possible to do this. I am manually copying all fields from main entity to Audit class.

    So, your input is appreciated.

    Thanks

    Thursday, October 18, 2012 4:40 AM

All replies

  • Hi cp.net,

    Could you please post more code here? How did you define your context?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 16, 2012 2:57 AM
    Moderator
  • public class ClassWithRowguid { public int ID { get; set; } public Guid MyRowGuid { get; set; } } public class ClassWithRowguidConfig : EntityTypeConfiguration<ClassWithRowguid> { public ClassWithRowguidConfig () { this.Property(m => m.IDN).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.HasKey(m => m.ID); this.Property(m => m.RowGuid).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } } public class Order : ClassWithRowguid { public int ID { get; set; } public string OrderedBy { get; set; } public virtual ICollection<OrderDetail> OrderDetails { get; set; } } public class OrderConfig : EntityTypeConfiguration<Order> { public OrderConfig () { this.Property(m => m.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.HasKey(m => m.ID); Map(m =>            {               

    m.MapInheritedProperties();               

    m.ToTable("Order");            });

    } } public class OrderDetail : ClassWithRowguid { public int ID { get; set; } public string ItemDesc { get; set; } public virtual Order Order{ get; set; } } public class OrderDetailConfig : EntityTypeConfiguration<OrderDetail> { public OrderDetailConfig () { this.Property(m => m.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); this.HasKey(m => m.ID); Map(m =>

                {
                    m.MapInheritedProperties();
                    m.ToTable("OrderDetail");
                });

    } } public class OrderDbContext : DbContext { public OrderDbContext() : base(DbCompiledModelFactory.CreateOrderModel()) { } public DbSet<Order> Orders{ get; set; } public DbSet<OrderDetail> OrderDetails{ get; set; } } public class DbCompiledModelFactory { public static DbCompiledModel CreateOrderModel() { Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", @"AppData", @"Data Source=D:\OrdersDb.sdf"); var builder = new DbModelBuilder();

    builder.Configurations.Add(new ClassWithRowguidConfig()); builder.Configurations.Add(new OrderConfig()); builder.Configurations.Add(new OrderDetailDetailConfig()); builder.Conventions.Remove <PluralizingTableNameConvention>(); var model = builder.Build(Database.DefaultConnectionFactory.CreateConnection("OrdersDb")).Compile(); return model; } }


    I want the Order and OrderDetail classes to have the MyRowGuid field but with DatabaseGeneratedOption.None

    • Edited by cp.net Tuesday, October 16, 2012 7:55 AM
    Tuesday, October 16, 2012 7:42 AM
  • Hi cp.net,

    If you're using TPC, the goal couldn't achieve. Why you want to achieve this? I may help you to re-design the entity definition.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 18, 2012 3:47 AM
    Moderator
  • Hi Allen,

    I am trying to create tables for Audit Trail to log updates to entity records. So each Audit class will be derived from main entity class to get all the fields, example Order_Audit derived from Order. So, if Order class has identity fields like 'AddedDate' and 'RowGuid', I do not want them as identity in the corresponding Order_Audit class since they will need to be updated with values from main entity table.

    So, right now I have given up on TPC because as you said, it is not possible to do this. I am manually copying all fields from main entity to Audit class.

    So, your input is appreciated.

    Thanks

    Thursday, October 18, 2012 4:40 AM