65.9K
CodeProject 正在变化。 阅读更多。
Home

使用Entity Framework 6访问NetSuite数据

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2投票s)

2019 年 5 月 22 日

CPOL
viewsIcon

2294

本文演示了如何使用 Entity Framework 代码优先方法访问 NetSuite 数据。Entity Framework 6 在 .NET 4.5 及以上版本中可用。

Entity Framework 是一个对象关系映射框架,可用于将数据作为对象进行处理。虽然您可以在 Visual Studio 中运行 ADO.NET 实体数据模型向导来处理实体模型的生成,但如果您的数据源发生变化,或者您希望更好地控制实体的操作方式,这种方法(模型优先方法)可能会让您处于劣势。在本文中,您将完成使用 CData ADO.NET Provider 访问 NetSuite 数据的代码优先方法。

  1. 打开 Visual Studio 并创建一个新的 Windows 窗体应用程序。本文使用一个 C# 项目,.NET 4.5。
  2. 在 Visual Studio 的程序包管理器控制台中运行命令 'Install-Package EntityFramework',以安装 Entity Framework 的最新版本。
  3. 修改项目中的 App.config 文件,添加对 NetSuite Entity Framework 6 程序集和连接字符串的引用。

    “身份验证”部分下的 User 和 Password 属性必须设置为有效的 NetSuite 用户凭据。此外,AccountId 必须设置为指定用户可以使用的公司帐户的 ID。RoleId 可以选择指定,以使用有限权限登录用户。

    有关连接到 NetSuite 的更多信息,请参阅帮助文档的“入门”章节。

    <configuration> 
      ... 
      <connectionStrings> 
        <add name="NetSuiteContext" connectionString="Offline=False;Account Id=XABC123456;Password=password;User=user;Role Id=3;Version=2018_1;" providerName="System.Data.CData.NetSuite" /> 
      </connectionStrings> 
      <entityFramework>
        <providers> 
          ... 
          <provider invariantName="System.Data.CData.NetSuite" type="System.Data.CData.NetSuite.NetSuiteProviderServices, System.Data.CData.NetSuite.Entities.EF6" /> 
        </providers> 
      <entityFramework> 
    </configuration>
  4. 添加对 System.Data.CData.NetSuite.Entities.EF6.dll 的引用,该文件位于安装目录的 lib -> 4.0 子文件夹中。
  5. 在此时构建项目以确保一切正常。完成后,您就可以开始使用 Entity Framework 编写代码了。
  6. 向项目中添加一个名为 .cs 的新文件,并向其中添加一个类。这将是您的数据库上下文,它将扩展 DbContext 类。在本例中,该类名为 NetSuiteContext。以下代码示例覆盖了 OnModelCreating 方法以进行以下更改
    • 从 ModelBuilder 约定中删除 PluralizingTableNameConvention
    • 删除对 MigrationHistory 表的请求。
    using System.Data.Entity; 
    using System.Data.Entity.Infrastructure; 
    using System.Data.Entity.ModelConfiguration.Conventions; 
    
    class NetSuiteContext : DbContext { 
      public NetSuiteContext() { } 
    
      protected override void OnModelCreating(DbModelBuilder modelBuilder) 
      { 
        // To remove the requests to the Migration History table 
        Database.SetInitializer<NetSuiteContext>(null); 
    
        // To remove the plural names
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
      } 
    }
  7. 创建另一个 .cs 文件,并以您要检索的 NetSuite 实体命名,例如 SalesOrder。在此文件中,定义实体和实体配置,这将类似于下面的示例
    using System.Data.Entity.ModelConfiguration; 
    using System.ComponentModel.DataAnnotations.Schema; 
    
    [System.ComponentModel.DataAnnotations.Schema.Table("SalesOrder")] 
    public class SalesOrder { 
      [System.ComponentModel.DataAnnotations.Key] 
      public System.String CustomerName { get; set; } 
      public System.String SalesOrderTotal { get; set; } 
    }
  8. 现在您已经创建了一个实体,请将该实体添加到您的上下文类中: 
    public DbSet<SalesOrder> SalesOrder { set; get; }
  9. 完成上下文和实体后,您现在就可以在单独的类中查询数据了。例如: 
    NetSuiteContext context = new NetSuiteContext(); 
    context.Configuration.UseDatabaseNullSemantics = true; 
    var query = from line in context.SalesOrder select line;

免费试用和更多信息

现在您已经看到了一个从 Entity Framework 应用程序连接和处理 NetSuite 数据的基本示例,请访问 NetSuite ADO.NET Provider 页面,阅读有关 CData NetSuite ADO.NET Provider 的更多信息。开始使用实时 NetSuite 数据构建代码优先的 Entity Framework 应用程序。与往常一样,世界一流的 CData 支持团队随时准备回答您可能遇到的任何问题。

© . All rights reserved.