locked
How to Maintain Unique Record in the DataBase RRS feed

  • Question

  • Hello ALL

    I have table as aspnet_Roles columns roleID,RoleName,Description.

    in this i need maintain one record rolename as SUPER USER

    and it should be unique and never to be delete and never to be fetch how i can get it and i need to do this in server side only any way for this??

     

    Thanks

    Tuesday, June 1, 2010 5:47 AM

Answers

  • Hi,

    The condition you use in the query interceptor can change over time. So if you know that you're "while Login" (whatever that means), you can return different condition than when you're in normal mode of operation. The query interceptor method will be called again for each request to get the condition, and so you can change its behavior per request. Same goes for the change interceptor.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, June 2, 2010 8:53 AM
    Moderator

All replies

  • You can use a QueryInterceptor method (http://msdn.microsoft.com/en-us/library/system.data.services.queryinterceptorattribute.aspx) to add a filter that makes sure that the record with rolename SUPER USER is never returned, and you can use a ChangeInterceptor (http://msdn.microsoft.com/en-us/library/system.data.services.changeinterceptorattribute.aspx) method to make sure that this record is never deleted, and that other records aren't inserted or updated with rolename SUPER USER.
    Tuesday, June 1, 2010 7:14 AM
  • Hi Sir,

    if i dont want to delete that record what i need to mention in ChangeInterceptorAttibute class , actually i am beginner to this can u please give some abstract code.

     

    Thanks.

    Tuesday, June 1, 2010 7:31 AM
  • Hi,

    Please try to use web search to find answers first. Very simple bing search for "changeinterceptor" returns this http://weblogs.asp.net/cibrax/archive/2009/06/08/injecting-custom-logic-in-ado-net-data-services.aspx as one of the first links. There a sample of change interceptor in there to prevent Add and Change operations, making this for Delete is pretty straighforward.

    Thanks,


    Vitek Karas [MSFT]
    Tuesday, June 1, 2010 7:59 AM
    Moderator
  • Hello Sir,

    I tried this according to my Requirement so i wrote like this on serverside.

     

     

    [QueryInterceptor("aspnet_Roles")]

            public Expression<Func<aspnet_Roles,bool>> filterRoles()

            {

                return apd => apd.RoleName == "Super User";

            }

     

            [ChangeInterceptor("aspnet_Roles")]

            public void chnageRoleName(aspnet_Roles o, UpdateOperations operation)

            {

                if(operation==UpdateOperations.Add || operation==UpdateOperations.Change)

                {

                    throw new DataServiceException(400,"You are not allowed to create or change a rolename with ' Super User' ");

                }

            }

     

    while going for insertion if i entered rolename other than SUPER USER also its not allowing me to save.

    here i got exception on serverside.

     

    and while going for Delete SUPER USER also not working

     

    for DeleteRole i wrote API like this on Client side

    public bool deleteRole()

    {

                aspnet_Roles asp = new aspnet_Roles();

                asp.RoleName = "SUPER USER";

                Guid id = new Guid("248bd054-e402-4e84-bad9-9f23bff1e5ef");

                Guid id1 = new Guid("370e09df-df28-465f-9c19-82bc5dc28fc1");

                asp.ApplicationId = id1;

                asp.RoleId = id;

                asp.LoweredRoleName = "physician";

                List<RolesInFeature> features= new List<RolesInFeature>();

                ff = container.RolesInFeatures.Where(p => p.RoleID == id).ToList();

                container.DeleteLink(asp, "RolesInFeatures", features);

                container.DeleteObject(asp);

           container.SaveChanges();

     }

     

     

     

    So here i am getting Exception on client side as DataServiceException on this line container.DeleteLink(asp, "RolesInFeatures", features);

    where i am Doing Mistake Sir?

     

    Thanks.

    Tuesday, June 1, 2010 10:28 AM
  • Hi,

    First, if you say it fails, please post at least the error message you get, it's very hard to guess.

    As for the query interceptor, that method should return a condition which if it returns "true" the given record will be returned, if it returns "false" the given record will be "hidden". So if you want to return all records except the SuperUser one the condition needs to look like this:

    return apd => apd.RoleName != "Super User";

    As for the change interceptor, your interceptor doesn't even look a the RoleName - it simply blocks all Add and Change operations. So the fact that it blocks any insert is expected. If you want to allow Add and Change on anything but the SuperUser you need something like

    if((operation==UpdateOperations.Add || operation==UpdateOperations.Change) && o.RoleName == "Super User")
    {
      throw new Exception(...);
    }

    Also note that this will not block Delete operations, but I assume you know how to fix that.

    One other side note: If you're posting code please use the Insert Code Block button (the right most one on the toolbar) and choose the language in which the code sample is. It will make it look much nicer and readable.

    Thanks,

     


    Vitek Karas [MSFT]
    Tuesday, June 1, 2010 1:42 PM
    Moderator
  • Hello Sir,

    Sure i will post my Error Message . Here i got my required output , he re is my Code.

    [QueryInterceptor("aspnet_Roles")]

            public Expression<Func<aspnet_Roles,bool>> filterRoles()

            {

                return apd => apd.RoleName != "Super User";

            }

     

            [ChangeInterceptor("aspnet_Roles")]

            public void chnageRoleName(aspnet_Roles o, UpdateOperations operation)

            {

                if ((operation == UpdateOperations.Add && o.RoleName == "Super User") || (operation == UpdateOperations.Delete && o.RoleName == "Super User"))

                {

                    throw new DataServiceException(400,"You are not allowed to Create or Delete a rolename with ' Super User' ");

                }

            }

     

    This is Working Perfect and Thanks for this, But here one problem 

    Super User task is to create Administrator, then administrator will do remaining task.

    So while Login i need to get Super User role also because we need to create Administrator this is the one where i need Super User Role.

    After this i dont want Super User in the entire Application i mean i dont want to fetch that record for that Above Code will work.

     

    Can i Over Come this?

     

    Thanks.

     

     

     

    Wednesday, June 2, 2010 3:56 AM
  • Hi,

    The condition you use in the query interceptor can change over time. So if you know that you're "while Login" (whatever that means), you can return different condition than when you're in normal mode of operation. The query interceptor method will be called again for each request to get the condition, and so you can change its behavior per request. Same goes for the change interceptor.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, June 2, 2010 8:53 AM
    Moderator
  • Hello Sir,

     

    Yes Its Working Fine. Thanks.

    Wednesday, June 2, 2010 9:55 AM