一个关于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
...全文
181 10 点赞 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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方法。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2013-02-20 10:12
社区公告

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