Base solution for your next web application
Open Closed

Default values for properties in entities #2387


User avatar
0
vnetonline created

Hi.

I have a simple class

public class Car : Entity{

public virtual DateTime AddDate {get;set;} // retrieved from the Session object public virtual DateTime UpdateDate { get;set;} public virtual string UserName{ get; set; } }

I am using the AsyncCrud Interface. However I want to factor out these properties in a base object and set them everytime the user creates or updates an entity. so that these are updated for all objects automatically. I cannot use the built in AuditedEntity class since this is a database first context.

I cannot seem to do this at the Core layer or the entity framework layer (plus session object is available on at the services layer). Can you please specify how this is achieved. I also do not want to pass this information in the DTO objects. It should be set by default whenever a new object is created or updated. Your help will be greatly appreciated.

Regards


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can set the properties you want by overriding ApplyAbpConcepts of your DbContext. See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/a68c430fb37067c88cedbf7b62411274579d223f/src/Abp.EntityFramework/EntityFramework/AbpDbContext.cs#L225">https://github.com/aspnetboilerplate/as ... xt.cs#L225</a>

    If you have a custom session, the you can resolve it in your overriden method and use it's properties. You can check how we used AbpSession in above link.

  • User Avatar
    0
    vnetonline created

    Thanks for directing me to the correct reference. I eventually ending up overriding the two methods namely SetCreationAuditProperties and SetModificationAuditProperties.