Whether there is a way to set property of entity class in global level?

4 posts, 0 answers
  1. Jin
    Jin avatar
    80 posts
    Member since:
    Jun 2012

    Posted 31 Aug 2012 Link to this post

    In my project, all tables all will have 2 fixed field:Last_Updated_By, Created_By. When I do insert or update operation, I will put the current login name as the value of two field. When I use OpenAceess ORM, whether there is a way to set the value in one place instead of set the value while in every time to handling entity?
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 04 Sep 2012 Link to this post

    Hello Jin,

    In order to be able to set those properties for all entities in a common way it would be easier if they have a base class or implement a common interface that contain the CreatedBy / LastUpdatedBy properties. Both approaches could be modeled with the visual designer, please feel free to ask us if you are unsure how to achieve this.

    Afterwards you could extend your context class with another partial class (just make sure the class name and namespace are exactly the same) and create an additional method which wraps the call to SaveChanged but also updates the user information for the inserted/updated objects in the current transaction. Then you can call this method instead of SaveChanges() on all places in the application where you are committing a transaction.
    The example below shows how the partial class extending the context would look like. In this case my test classes implement a common interface named IUserInfo:
    namespace Model
    {
        public partial class EntitiesModel
        {
            public void SaveChangesAndUpdateUserInfo()
            {
                var changes = this.GetChanges();
                foreach (IUserInfo inserted in changes.GetInserts<IUserInfo>())
                {
                    inserted.CreatedBy = "User1";
                    inserted.UpdatedBy = "User1";
                }
                foreach (IUserInfo updated in changes.GetUpdates<IUserInfo>())
                {
                    updated.UpdatedBy = "User1";
                }
     
                base.SaveChanges();
            }
        }
     }

    Hope that helps.

    Regards,
    Alexander
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. Sambathraj
    Sambathraj avatar
    16 posts
    Member since:
    Jan 2011

    Posted 16 Jan 2014 Link to this post

    hi,

    I have similar need. It would be great to see a example project for this. Can you please illustrate how to use the visual designer for this

    purpose?



    I am using the latest version of ORM.



    Thanks,

    Sambath

  5. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 21 Jan 2014 Link to this post

    Hi Sambathraj,

    I am afraid that there is no way to implement the same scenario as Alexander described previously using the Visual Designer. The designer is suitable for static design time facts and not for dynamic runtime ones like current user name or date time values.

    If you have hard time implementing your scenario, please open a new support or forum thread with more details describing the exact situation so we can better assist you.

    Regards,
    Viktor Zhivkov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top