一个关于LINQ TO SQL的问题,DATACONTEXT类没有ADD方法?
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