首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-02-25 
一个关于LINQ TO SQL的问题,DATACONTEXT类没有ADD方法?using Systemusing System.Collections.Genericus

一个关于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.cs16626Simple Linq to SQL
[解决办法]
InsertObject方法,或者是AddObject方法。
[解决办法]
Add 是早期版本的LINQ里的扩展方法

现在的LINQ里改成InsertOnSubmit() 了
[解决办法]
InsertOnSubmit方法


dc.Customers.InsertOnSubmit(stu); 
dc.SubmitChanges(); 

热点排行