How to redefine the functionality of fields of base classes?

Well i have BaseModel

public class BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public bool Deleted { get; set; }
}

I also have a model Update

public class Update : BaseModel
{
    public DateTime Updated { get; set; }
}

which is obtained from the BaseModel In field BaseModel Iddoes not increase, but for the model UpdateI need Idwith Identity.

Is it possible to add a new field in Updatehow UpdateId, which will increase, but delete the field Id?


Is this possible?

How to simply declare models as follows

public class BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public bool Deleted { get; set; }
}

and

public class Update : BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public DateTime Updated { get; set; }
}

Idin Updateoverride IdinBaseModel

+4
source share
3 answers

( Windows Forms ):

public class BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public virtual int Id { get; set; }
    public bool Deleted { get; set; }
}

public class Update : BaseModel
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public override int Id { get { return base.Id; } set { base.Id = value; } }
    public DateTime Updated { get; set; }
}
+2

< > BaseModel UpdateModel public class UpdateModel : BaseModel { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public new int Id { get; set; } public DateTime Updated { get; set; } }

, , Id, , UpdateModel BaseModel, , .

public class BaseEntity
{
    public int Id { get; set; }
    public bool Deleted { get; set; }
}
public class UpdateEntity : BaseEntity
{
    public DateTime Updated { get; set; }
}

public class Entity1 : BaseEntity
{
    public string Name { get; set; }
}
public class Entity2 : UpdateEntity
{
    public string Name { get; set; }
}

public abstract class BaseEntityTypeConfiguration<T> : EntityTypeConfiguration<T>
    where T : BaseEntity
{
    public BaseEntityTypeConfiguration()
    {
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    }
}

public abstract class UpdateEntityTypeConfiguration<T> : EntityTypeConfiguration<T>
    where T : UpdateEntity
{
    public UpdateEntityTypeConfiguration()
    {
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class Entity1Configuration : BaseEntityTypeConfiguration<Entity1>
{
    public Entity1Configuration()
        : base()
    {
        Property(x => x.Name).HasMaxLength(100);
    }
}
public class Entity2Configuration : UpdateEntityTypeConfiguration<Entity2>
{
    public Entity2Configuration()
        : base()
    {
        Property(x => x.Name).HasMaxLength(100);
    }
}

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var typesToRegister = from type in Assembly.GetExecutingAssembly().GetTypes()
                              where !string.IsNullOrEmpty(type.Namespace) &&
                                    type.BaseType != null &&
                                    type.BaseType.IsGenericType
                              let genericType = type.BaseType.GetGenericTypeDefinition()
                              where genericType == typeof(BaseEntityTypeConfiguration<>) || genericType == typeof(UpdateEntityTypeConfiguration<>)
                              let genericArgument = type.BaseType.GetGenericArguments().FirstOrDefault()
                              where genericArgument != null && genericArgument.BaseType != null &&
                              (genericArgument.BaseType == typeof(BaseEntity) || genericArgument.BaseType == typeof(UpdateEntity))
                              select type;

        foreach (var type in typesToRegister)
        {
            dynamic configurationInstance = Activator.CreateInstance(type);
            modelBuilder.Configurations.Add(configurationInstance);
        }

        base.OnModelCreating(modelBuilder);
    }
}
+1

class ( DbContext) Update, OnModelCreating :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Update>()
        .Property(x => x.Id)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

}

:

You can also customize your model using data annotations or a free API. Priority is given to configuration through a free API, followed by data annotations and then agreements.

This means that you can override the data annotation configuration using the free API (via OnModelCreating).

+1
source

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


All Articles