日期:2014-05-20  浏览次数:20923 次

求助:Linq to SQL设计器创建的类的属性缺少Add()方法
我用Linq to SQL设计器创建的类的属性缺少Add()方法,请高手帮忙看看代码,红色是错误的地方,是按书上打的。

dc.Customers.Add(douglas);

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Modifying_Data_Using_Linq_To_SQL
{
    //主程序
    public class Tester
    {
        static void Main()
        {
            AddCustomer();
            UpdateCustomer();
            Console.ReadKey();
        }
        
        private static void AddCustomer()
        {
            Console.WriteLine("Adding a new customer...");
            MyNewItemDataContext dc = new MyNewItemDataContext();

            //加入带有地址的新客户
            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 = "1c 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...");
            MyNewItemDataContext dc = new MyNewItemDataContext();
            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 = "Los 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)
        {
            MyNewItemDataContext dc = new MyNewItemDataContext();
            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}", Title, FirstName, LastName, EmailAddress);
            foreach (CustomerAddress ca in CustomerAddresses)
            {
                sb.AppendFormat("\n\t{0}: {1}, {2}",
                    ca.AddressType, ca.Address.AddressLine1, ca.Address.City);
            }
            sb.AppendLine();
            return sb.ToString();
        }
    }
}


错误提示:“System.Data.Linq.Table<Modifying_Data_Using_Linq_To_SQL.Customer>”不包含“Add”的定义,并且找不到可接受类型为“System.Data.Linq.Table<Modifying_Data_Using_Linq_To_SQL.Customer>”的第一个参数的扩展方法“Add”(是否缺少 using 指令或程序集引用?)

------解决方案--------------------
dc.Customers.Add(douglas);
这句里面的Customers表,
和douglas(Customer )是一致的吗????
------解决方案--------------------
用 dc.Customers.InsertOnSubmit(douglas)方法