none
Alternative to AFTER INSERT Trigger RRS feed

  • Question

  • I have an appointment scheduling application implemented using Silverlight 4, WCF Ria Services, EF 4 and SQL 2008.    

    I have an AFTER INSERT trigger on the appointments table with a validation that prevents double-booking appointments. Ideally I would like this validation to be performed in the EF layer when I am submitting context additions instead of having the business logic in the database. The challenge is the context may not contain appointments recently added by other users so relying on a validation using appointments in the context is not acceptable.   

    Any help or suggestions would be greatly appreciated.    

    Wednesday, October 5, 2011 2:41 PM

Answers

  • Hi,

    You can perform this validation just before saving the entity through context...In ObjectCOntext, SaveChanges event available for this type of purpose.

     partial void OnContextCreated()
            {
                SavingChanges += new EventHandler(TestTrailEntities_SavingChanges);
            }
    
            void TestTrailEntities_SavingChanges(object sender, EventArgs e)
            {
                var newEntityEntries = ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added);
                foreach (var item in newEntityEntries)
                {
                    //perform validations and fail saving
                }
            }
    
    Hope this...helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, October 5, 2011 3:46 PM
  • Hi JohnnieCatz,

    Welcome!

    If you use EF4.1, you can use Custom validation sample: Uniqueness: http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 6:52 AM
    Moderator

All replies

  • Hi,

    You can perform this validation just before saving the entity through context...In ObjectCOntext, SaveChanges event available for this type of purpose.

     partial void OnContextCreated()
            {
                SavingChanges += new EventHandler(TestTrailEntities_SavingChanges);
            }
    
            void TestTrailEntities_SavingChanges(object sender, EventArgs e)
            {
                var newEntityEntries = ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added);
                foreach (var item in newEntityEntries)
                {
                    //perform validations and fail saving
                }
            }
    
    Hope this...helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, October 5, 2011 3:46 PM
  • Hi JohnnieCatz,

    Welcome!

    If you use EF4.1, you can use Custom validation sample: Uniqueness: http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 6:52 AM
    Moderator
  • Thanks to both of you for responding. I will try these suggestions out and report my findings. 
    John Sherwin
    Thursday, October 6, 2011 6:59 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 11, 2011 1:50 AM
    Moderator