如何访问其他类的灯开关中的数据库?

发布于 2024-12-20 03:01:51 字数 222 浏览 8 评论 0原文


我只是不知道如何清楚地解释这一点。所以我根据我所做的事情创建了一个简单的图像模式。
我的问题是,我如何才能访问 LS 中其他班级的数据库?
我一直在网上搜索,但没有找到任何解决方案。我希望我能在这里找到它。
谢谢!。 在此处输入图像描述


任何建议都已经受到赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初懵 2024-12-27 03:01:52

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

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

这些步骤取自 Lightswitch 帮助网站

  1. 导航到 ..ServerGenerated\GenerateArtifacts(在 LightSwitch 项目中),然后单击 ApplicationData.cs 并添加为链接。

  2. 在下面添加以下代码,此代码动态创建与数据库的连接。 LightSwitch 使用“_IntrinsicData”作为连接字符串。

    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;
        }
    } 

您应该能够通过 Context.adminusers 访问它

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

你不是我要的菜∠ 2024-12-27 03:01:51

感谢布莱恩的回答,但我在这里找到了问题的答案 Richard Waddell

这是我为实现目标所做的事情。

  1. 将您的 LS 项目切换到文件视图
  2. 转到“Common”项目,在“UserCode”文件夹下,创建一个类(例如 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()
谢谢!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文