Question


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.

Was it helpful?

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!

OTHER TIPS

The main difference is the first set of code that works is running inside a screen. For your Authenticate class, you need to do the following steps to access the Database.

Note: I'm assuming that your datasource has the default name of ApplicationData since you hid the name, if not, make the corresponding changes. If it's a completely different datasource, change "_IntrinsicData" in the steps below)

These steps are taken from the Lightswitch Help Website

  1. Navigate to ..ServerGenerated\GeneratedArtifacts (in the LightSwitch project) and click on ApplicationData.cs and Add As Link.

  2. Add the following code below, this code dynamically creates a connection to the database. LightSwitch uses “_IntrinsicData” as it’s connection string.

    private ApplicationDataObjectContext m_context;
    public ApplicationDataObjectContext Context
    {
        get
        {
            if (this.m_context == null)
            {
                string connString =
                    System.Web.Configuration.WebConfigurationManager
                    .ConnectionStrings["_IntrinsicData"].ConnectionString;
                EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                builder.Metadata =
                    "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
                builder.Provider =
                    "System.Data.SqlClient";
                builder.ProviderConnectionString = connString;
                this.m_context = new ApplicationDataObjectContext(builder.ConnectionString);
            }
            return this.m_context;
        }
    } 

You should be able to access it through Context.adminusers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top