Search code examples
c#switch-statementvisual-studio-lightswitchlslight

How to access database in lightswitch in other class?



I just don't know how to explain this clearly. So I create a simple image pattern of what I did.
My question is, how would I be able to access my database in other class in LS?
I've been searching on net, but I didn't found any solution. I hope I'll find it here.
Thanks!. enter image description here


Any suggestion is already appreciated.


Solution

  • Thanks for the answer Bryan, but I found the answer on my problem here Richard Waddell


    Here is what I did to achieve my goal.

    1. Switch your LS project to file view
    2. Go to "Common" project, under "UserCode" folder, create a class (e.g. Authenticate.cs) and put this codes.

    The code follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.LightSwitch;
    namespace LightSwitchApplication
    {
        public class Authenticate
        {
            public static adminuser GetCurrentUser()
            {
                adminuser userFound = (from useritem in 
                Application.Current.CreateDataWorkspace().basecampcoreData.adminusers
                where useritem.LoginID == Application.Current.User.Name
                select useritem).SingleOrDefault();
    
                if (userFound != null)
                    return userFound;
                else
                    return null;
            }
        }
    }
    

    Then you can now call the Authenticate.GetCurrentUser() anywhere in the project.
    Thanks!