【问题标题】:Linq to SQL vs Entity Data Model: Getting a stored proc to runLinq to SQL 与实体数据模型:让存储过程运行
【发布时间】:2010-08-20 22:09:05
【问题描述】:

我之前是否在使用 Silverlight 和 RIA,我使用的是 ADO.NET 实体数据模型和域上下文。我正在自学如何从 RIA 运行存储过程,但无法让它与以前的设置一起使用,因此我开始尝试使用 LINQ to SQL 方法。我做了以下事情:

  • 添加了 DBML
  • 拖放我的表和示例存储过程
  • 保存 DBML 并构建并重建解决方案和项目文件。
  • 创建域服务并尝试添加 dbml。

这是我的 DBML

这是我的错误。 可用的 DataContext/ObjectContext 类 列表显示为空。我已经重建了很多次,甚至重新启动了 Visual Studio。

所以这是我的问题:

  1. 是否有一种简单的方法可以使用 ADO.Net 执行存储过程,但我错过了该方法,因此我可以跳过添加 LINQtoSQL 类。
  2. 我是否遗漏了一些非常简单的方法来添加 dbml 类并以这种方式添加存储过程。

代码

ZipCodeDataClasses.dbml.layout

<?xml version="1.0" encoding="utf-8"?>
<ordesignerObjectsDiagram dslVersion="1.0.0.0" absoluteBounds="0, 0, 11, 8.5" name="ZipCodeDataClasses">
  <DataContextMoniker Name="/ZipCodeDataClassesDataContext" />
  <nestedChildShapes>
    <classShape Id="de7acfae-754b-4577-b227-3a33198fdc95" absoluteBounds="7.125, 2.375, 2, 1.3862939453125">
      <DataClassMoniker Name="/ZipCodeDataClassesDataContext/ZipCode" />
      <nestedChildShapes>
        <elementListCompartment Id="8744ad5e-7b77-4770-911d-6dc963876e40" absoluteBounds="7.14, 2.835, 1.9700000000000002, 0.8262939453125" name="DataPropertiesCompartment" titleTextColor="Black" itemTextColor="Black" />
      </nestedChildShapes>
    </classShape>
  </nestedChildShapes>
</ordesignerObjectsDiagram>

ZipCodeDataClasses.designer.cs

#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace StoredProcedureTest.Web
{
 using System.Data.Linq;
 using System.Data.Linq.Mapping;
 using System.Data;
 using System.Collections.Generic;
 using System.Reflection;
 using System.Linq;
 using System.Linq.Expressions;
 using System.ComponentModel;
 using System;


 [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="ZIPCODES")]
 public partial class ZipCodeDataClassesDataContext : System.Data.Linq.DataContext
 {

  private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

    #region Extensibility Method Definitions
    partial void OnCreated();
    #endregion

  public ZipCodeDataClassesDataContext() : 
    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["ZIPCODESConnectionString"].ConnectionString, mappingSource)
  {
   OnCreated();
  }

  public ZipCodeDataClassesDataContext(string connection) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }

  public ZipCodeDataClassesDataContext(System.Data.IDbConnection connection) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }

  public ZipCodeDataClassesDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }

  public ZipCodeDataClassesDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
    base(connection, mappingSource)
  {
   OnCreated();
  }

  public System.Data.Linq.Table<ZipCode> ZipCodes
  {
   get
   {
    return this.GetTable<ZipCode>();
   }
  }

  [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.sp_GetCityByZip")]
  public int sp_GetCityByZip([global::System.Data.Linq.Mapping.ParameterAttribute(Name="ZIP", DbType="NVarChar(10)")] string zIP, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Return", DbType="NVarChar(26)")] ref string @return)
  {
   IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), zIP, @return);
   @return = ((string)(result.GetParameterValue(1)));
   return ((int)(result.ReturnValue));
  }
 }

 [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ZipCodes")]
 public partial class ZipCode
 {

  private string _City;

  private string _State;

  private int _Zip;

  public ZipCode()
  {
  }

  [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_City", DbType="VarChar(25)")]
  public string City
  {
   get
   {
    return this._City;
   }
   set
   {
    if ((this._City != value))
    {
     this._City = value;
    }
   }
  }

  [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_State", DbType="VarChar(2)")]
  public string State
  {
   get
   {
    return this._State;
   }
   set
   {
    if ((this._State != value))
    {
     this._State = value;
    }
   }
  }

  [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Zip", DbType="Int NOT NULL")]
  public int Zip
  {
   get
   {
    return this._Zip;
   }
   set
   {
    if ((this._Zip != value))
    {
     this._Zip = value;
    }
   }
  }
 }
}
#pragma warning restore 1591

【问题讨论】:

    标签: linq-to-sql ado.net silverlight-4.0 c#-4.0 wcf-ria-services


    【解决方案1】:

    我放弃了让存储过程与 Silerlight 一起工作的尝试,但遵循本指南是我最接近实际实现的一次。希望对你有帮助。

    http://anhonga.wordpress.com/2010/02/17/using-stored-procedures-with-silverlight-4riapocos-part-ii/

    如果您找到实现存储过程的简单方法,请告诉我,因为我正在用 LINQ 重写数百个过程。

    【讨论】:

    • 感谢您的链接。证明有帮助。我找到了一个视频,它可以帮助我更好地理解发生了什么,希望它也能帮助你。 msdn.microsoft.com/en-us/vcsharp/dd565862.aspx
    • 感谢您的链接。视频看起来不错,我将不得不尝试实现,看看我是否可以让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多