如何设置定义文件以在 Azure 函数中使用 LINQ?
How to setup the definition file to use LINQ in an Azure Function?
我创建了一个连接到 Azure SQL 数据库并执行直接 SQL 命令的 Azure 函数。现在我想改用 LINQ。我使用 Visual Studios 创建了定义文件,但我不确定如何在上传到函数后引用定义文件。下面的代码出现错误:
error CS1061: 'SqlConnection' does not contain a definition for 'Customers' and no extension method 'Customers' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference?)
这是我的 Azure 函数代码:
using System.Net;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
var connection = new SqlConnection(cnnString);
var result = from r in connection.Customers select r;
foreach (var r in result)
{
log.Info(r.squid);
}
log.Info("Log added to database successfully!");
return req.CreateResponse(HttpStatusCode.OK);
}
您可以使用#load 加载另一个class。您可以使用代码底部的 view files
Link 创建新文件 window
Run.csx 文件
#r "System.Data.Linq"
#load "TodoItem.csx"
using System.Net;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;
public static void Run(string input, TraceWriter log)
{
var cnnString = ConfigurationManager.ConnectionStrings["MS_TableConnectionString"].ConnectionString;
DataContext db = new DataContext(cnnString);
Table<todoItem> todoItems = db.GetTable<todoItem>();
IQueryable<todoItem> itemQuery = from todoItem in todoItems select todoItem;
foreach (todoItem item in itemQuery)
{
log.Info($"ID={item.id}, Text={item.text}");
}
return;
}
TodoItem.csx 文件
#r "System.Data.Linq"
#r "System.Data"
using System.Data.Linq.Mapping;
using System.Data;
[Table(Name = "TodoItem")]
public class todoItem
{
private string _id;
[Column(IsPrimaryKey=true, Storage="_id")]
public string id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
private string _text;
[Column(Storage="_text")]
public string text
{
get
{
return this._text;
}
set
{
this._text=value;
}
}
}
我创建了一个连接到 Azure SQL 数据库并执行直接 SQL 命令的 Azure 函数。现在我想改用 LINQ。我使用 Visual Studios 创建了定义文件,但我不确定如何在上传到函数后引用定义文件。下面的代码出现错误:
error CS1061: 'SqlConnection' does not contain a definition for 'Customers' and no extension method 'Customers' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference?)
这是我的 Azure 函数代码:
using System.Net;
using Dapper;
using System.Data.SqlClient;
using System.Configuration;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
var connection = new SqlConnection(cnnString);
var result = from r in connection.Customers select r;
foreach (var r in result)
{
log.Info(r.squid);
}
log.Info("Log added to database successfully!");
return req.CreateResponse(HttpStatusCode.OK);
}
您可以使用#load 加载另一个class。您可以使用代码底部的 view files
Link 创建新文件 window
Run.csx 文件
#r "System.Data.Linq"
#load "TodoItem.csx"
using System.Net;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Configuration;
public static void Run(string input, TraceWriter log)
{
var cnnString = ConfigurationManager.ConnectionStrings["MS_TableConnectionString"].ConnectionString;
DataContext db = new DataContext(cnnString);
Table<todoItem> todoItems = db.GetTable<todoItem>();
IQueryable<todoItem> itemQuery = from todoItem in todoItems select todoItem;
foreach (todoItem item in itemQuery)
{
log.Info($"ID={item.id}, Text={item.text}");
}
return;
}
TodoItem.csx 文件
#r "System.Data.Linq"
#r "System.Data"
using System.Data.Linq.Mapping;
using System.Data;
[Table(Name = "TodoItem")]
public class todoItem
{
private string _id;
[Column(IsPrimaryKey=true, Storage="_id")]
public string id
{
get
{
return this._id;
}
set
{
this._id = value;
}
}
private string _text;
[Column(Storage="_text")]
public string text
{
get
{
return this._text;
}
set
{
this._text=value;
}
}
}