This is a migrated thread and some comments may be shown as answers.

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

3 Answers 25 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jin
Top achievements
Rank 1
Jin asked on 31 Aug 2012, 06:35 AM
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?

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 04 Sep 2012, 04:12 PM
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!
0
Sambathraj
Top achievements
Rank 1
answered on 17 Jan 2014, 02:33 AM

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

0
Viktor Zhivkov
Telerik team
answered on 21 Jan 2014, 11:45 AM
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!
Tags
General Discussions
Asked by
Jin
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Sambathraj
Top achievements
Rank 1
Viktor Zhivkov
Telerik team
Share this question
or