如何:使用管理对象模型创建实体

实体是业务应用程序中的业务对象,例如客户和销售订单。本主题演示如何使用管理对象模型来创建实体。示例还为新的实体创建标识符。

本主题演示如何使用对象模型创建实体。用您的共享服务提供程序 (SSP) 名称替换代码中的 EnterYourSSPNameHere。另外,按照如何:使用管理对象模型创建 LobSystem中的演示,创建一个 LobSystem 实例并设置连接参数。

示例

本示例从 AdventureWorks2000 数据库创建一个称为 ProductModel 的实体。

先决条件

  • 确保已创建一个共享服务提供程序。

  • 按照如何:使用管理对象模型创建 LobSystem中的演示,创建一个 LobSystem 实例并设置连接参数。

  • 用您的共享资源提供程序名称替换代码中的常量值 EnterYourSSPNameHere。

项目引用

运行此示例之前,在控制台应用程序的代码项目中添加下面的项目引用:

  • Microsoft.SharePoint

  • Microsoft.SharePoint.Portal

  • Microsoft.Office.Server

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.ApplicationRegistry.Administration;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using WSSAdmin = Microsoft.SharePoint.Administration;
using OSSAdmin = Microsoft.Office.Server.Administration;

namespace Microsoft.SDK.SharePointServer.Samples
{
    class GetStartedAndCreateSystem
    {
        const string yourSSPName ="EnterYourSSPNameHere";

        static void Main(string[] args)
        {
            SetupBDC();
            CreateEntity();
            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }
        static void SetupBDC()
        {
            SqlSessionProvider.Instance().SetSharedResourceProviderToUse(yourSSPName);
        }
        static void CreateEntity()
        {
            LobSystemInstance mySysInstance = null;
            LobSystemInstanceCollection sysInsCollection = ApplicationRegistry.Instance.GetLobSystemInstancesLikeName("AdventureWorksSampleFromCode");
            foreach (LobSystemInstance sysInstance in sysInsCollection)
            {
                if (sysInstance.Name == "AdventureWorksSampleFromCode")
                {
                    mySysInstance = sysInstance;
                    break;
                }
            }
            IList<Entity> entityCollection = new List<Entity>(mySysInstance.LobSystem.Entities);
            Entity newEntity = mySysInstance.LobSystem.Entities.Create("ProductModel", true);
            EntityCollection entityColl = mySysInstance.LobSystem.Entities;
            foreach (Entity entity in entityColl)
            {
                if (entity.Name == "ProductModel")
                {
                    entity.Identifiers.Create("ProductModelID", true, "System.Int32");
                    break;
                }
            }
            Console.WriteLine("Created the entity successfully.");
        }
    }
}

See Also

概念

业务数据目录:元数据模型