添加 OData Client Code Generator 扩展
添加OData T4生成工具
修改 T4 模板的 MetadataDocumentUri
运行Web项目,之后重新保存 T4模板,保存成功后会生成代理类 (必须保证项目运行或把项目发布到IIS,保证URL可以正常访问)
操作代码如下:
static void Main(string[] args)
{
const string serviceUri = "http://localhost:1088/OData";
var container = new Container(new Uri(serviceUri));
GetProducts(container); var pro = new Product {Name = "Client OData", Price = , Category = "IT"};
container.AddToProducts(pro); pro = new Product { Name = "Client OData", Price = , Category = "IT" };
container.AddToProducts(pro); var list = container.Products.Where(c=>c.Id==).ToList();
var proUpdate = list.FirstOrDefault();
if (proUpdate != null)
{
proUpdate.Name = "Update Client OData";
}
container.UpdateObject(proUpdate); container.AddAndUpdateResponsePreference = DataServiceResponsePreference.IncludeContent;
// Add the data to the server var response = container.SaveChanges(SaveChangesOptions.ReplaceOnUpdate); foreach (var operationResponse in response)
{
Console.WriteLine("Response: {0}", operationResponse.StatusCode);
} Console.ReadLine(); }