一个关于LINQ TO SQL的问题,DATACONTEXT类没有ADD方法?

jbqiu168 2013-02-20 10:12:59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using System.IO;




namespace Simple_Linq_to_SQL
{
public class tester
{
static void Main()
{

AddCustomer();
UpdateCustomer();
Console.ReadKey();
}
private static void AddCustomer()
{
Console.WriteLine("Adding a new customer...");
AdventureWorksAddressDataContext dc = new AdventureWorksAddressDataContext();
dc.Log = Console.Out;
Customer douglas = new Customer();
douglas.FirstName = "douglas";
douglas.LastName = "adams";
douglas.EmailAddress = "douglas0@adventureworks.com";
douglas.PasswordHash = "fake";
douglas.PasswordSalt = "fake";
douglas.ModifiedDate = DateTime.Today;
douglas.rowguid = Guid.NewGuid();

Address addr = new Address();
addr.AddressLine1 = "lc sharp way";
addr.City = "seattle";
addr.PostalCode = "98011";
addr.StateProvince = "washington";
addr.CountryRegion = "united states";
addr.ModifiedDate = DateTime.Today;
addr.rowguid = Guid.NewGuid();
CustomerAddress ca = new CustomerAddress();
ca.AddressType = "main office";
ca.Address = addr;
ca.Customer = douglas;
ca.ModifiedDate = DateTime.Today;
ca.rowguid = Guid.NewGuid();

dc.Customers.Add(douglas);
dc.SubmitChanges();
ShowCustomersByFirstName("Douglas");
}
private static void UpdateCustomer()
{
Console.WriteLine("Updating a customer...");
AdventureWorksAddressDataContext dc = new AdventureWorksAddressDataContext();
dc.Log = Console.Out;
Customer dAdams = dc.Customers.Single(c => (c.FirstName == "Douglas" && c.LastName == "Adams"));
Console.WriteLine("Before:\n(0)", dAdams);
dAdams.Title = "Mr.";
Address addr = new Address();
addr.AddressLine1 = "1 Warehouse Place";
addr.City = "Log Angeles";
addr.PostalCode = "30210";
addr.StateProvince = "California";
addr.CountryRegion = "United States";
addr.ModifiedDate = DateTime.Today;
addr.rowguid = Guid.NewGuid();

CustomerAddress ca = new CustomerAddress();
ca.AddressType = "Shipping";
ca.Address = addr;
ca.Customer = dAdams;
ca.ModifiedDate = DateTime.Today;
ca.rowguid = Guid.NewGuid();
dc.SubmitChanges();
Customer dAdams1 = dc.Customers.Single(c => (c.FirstName == "Douglas" && c.LastName == "Adams"));
Console.WriteLine("After:\n(0)", dAdams);
}
private static void ShowCustomersByFirstName(string firstName)
{
AdventureWorksAddressDataContext dc = new AdventureWorksAddressDataContext();
var customers = from customer in dc.Customers
where customer.FirstName == "Douglas"
orderby customer.FirstName, customer.LastName
select customer;
Console.WriteLine("Customers whose first name is {0}:", firstName);
foreach (Customer customer in customers)
Console.WriteLine(customer);
}
}
public partial class Customer
{
public override string ToString()
{

StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0} {1} {2} {3}", FirstName, LastName, EmailAddress, Phone);
foreach (CustomerAddress ca in CustomerAddresses)
{
sb.AppendFormat("\n\t{0},{1}", ca.Address.AddressLine1, ca.Address.City);
}
sb.AppendLine();
return sb.ToString();
}
}
}
错误 1 “System.Data.Linq.Table<Simple_Linq_to_SQL.Customer>”不包含“Add”的定义,并且找不到可接受类型为“System.Data.Linq.Table<Simple_Linq_to_SQL.Customer>”的第一个参数的扩展方法“Add”(是否缺少 using 指令或程序集引用?) f:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\Simple Linq to SQL\Simple Linq to SQL\Program.cs 166 26 Simple Linq to SQL
...全文
273 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mahaijun125527 2013-05-25
  • 打赏
  • 举报
回复
东东东东
mahaijun125527 2013-05-25
  • 打赏
  • 举报
回复
东东
EnForGrass 2013-02-21
  • 打赏
  • 举报
回复
InsertOnSubmit方法 dc.Customers.InsertOnSubmit(stu); dc.SubmitChanges();
jbqiu168 2013-02-21
  • 打赏
  • 举报
回复
我用的是Vs2008版
jbqiu168 2013-02-21
  • 打赏
  • 举报
回复
我用的是Vs2008版
jbqiu168 2013-02-21
  • 打赏
  • 举报
回复
我用的是Vs2008版
q107770540 2013-02-21
  • 打赏
  • 举报
回复
Add 是早期版本的LINQ里的扩展方法 现在的LINQ里改成InsertOnSubmit() 了
jbqiu168 2013-02-21
  • 打赏
  • 举报
回复
能不能细说下,怎么写Add方法。
jbqiu168 2013-02-20
  • 打赏
  • 举报
回复
能不能细说下
threenewbee 2013-02-20
  • 打赏
  • 举报
回复
InsertObject方法,或者是AddObject方法。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧