Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在其他类中访问lightswitch中的数据库?_C#_Switch Statement_Visual Studio Lightswitch_Ls_Light - Fatal编程技术网

C# 如何在其他类中访问lightswitch中的数据库?

C# 如何在其他类中访问lightswitch中的数据库?,c#,switch-statement,visual-studio-lightswitch,ls,light,C#,Switch Statement,Visual Studio Lightswitch,Ls,Light,我只是不知道如何解释清楚。所以我创建了一个简单的图像模式。 我的问题是,我怎样才能访问LS中其他类中的数据库? 我一直在网上搜索,但没有找到任何解决办法。我希望我能在这里找到它。 谢谢 任何建议都是值得赞赏的。主要区别在于第一组代码在屏幕内运行。对于您的Authenticate类,您需要执行以下步骤来访问数据库 注意:我假设您的数据源具有默认名称ApplicationData,因为您隐藏了名称,如果没有,则进行相应的更改。如果是完全不同的数据源,请在以下步骤中更改“\u Intrins


我只是不知道如何解释清楚。所以我创建了一个简单的图像模式。
我的问题是,我怎样才能访问LS中其他类中的数据库?
我一直在网上搜索,但没有找到任何解决办法。我希望我能在这里找到它。
谢谢



任何建议都是值得赞赏的。

主要区别在于第一组代码在屏幕内运行。对于您的Authenticate类,您需要执行以下步骤来访问数据库

注意:我假设您的数据源具有默认名称ApplicationData,因为您隐藏了名称,如果没有,则进行相应的更改。如果是完全不同的数据源,请在以下步骤中更改“\u IntrinsicData”)

这些步骤是从

  • 导航到..ServerGenerated\GeneratedArtifacts(在LightSwitch项目中),单击ApplicationData.cs并添加为链接

  • 在下面添加以下代码,此代码将动态创建到数据库的连接。LightSwitch使用“\u IntrinsicData”作为其连接字符串


  • 你应该可以通过上下文访问它。管理员用户

    谢谢你的回答Bryan,但是我在这里找到了我问题的答案


    以下是我为实现我的目标所做的

  • 将LS项目切换到文件视图
  • 转到“通用”项目,在“用户代码”文件夹下,创建一个类(例如
    Authenticate.cs
    )并放置这些代码
  • 守则如下:

    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;
            }
        }
    }
    
    现在,您可以在项目中的任何位置调用
    Authenticate.GetCurrentUser()


    谢谢

    感谢您分享您是如何做到这一点的:)
    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;
            }
        }
    }