In one sample program (VS2012) I create a DataContext against the Nothwind DB <Customers> table, and I can run the following Xml.Linq query just fine. But in the next program I create a DbContext using an Entities model, and the same query no longer runs. How can I modify the DbContext sample so that it runs like the DataContext sample (or should I modify the query)? One difference I noticed between the DataContext sample and the DbContext sample is that the DbContext sample does not have
this.GetTable<Customer>()
which is where I'm having the problem with the Xml.Linq query in the DbContext sample
Source code here and picture of error below that
--DataContext Sample -- this one runs fine
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Drawing; using System.Xml.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace practiceVS2012stuff { public partial class frmXMLfromSqlData : Form { public frmXMLfromSqlData() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Northwind northwind = new Northwind(); XDocument xml = new XDocument( new XComment("First 3 customers starting with 'C'"), new XElement("Customers", (from cust in northwind.Customers where cust.CompanyName.StartsWith("C") orderby cust.CompanyName select new XElement("Customer", new XElement("CustomerID", cust.CustomerID), new XElement("CompanyName", cust.CompanyName), new XElement("ContactTitle", cust.ContactTitle), new XElement("ContactName", cust.ContactName), new XElement("Address", cust.Address), new XElement("City", cust.City), new XElement("Region", cust.Region), new XElement("PostalCode", cust.PostalCode), new XElement("Country", cust.Country), new XElement("Phone", cust.Phone), new XElement("Fax", cust.Fax))).Take(3))); Console.WriteLine(xml.ToString()); } } public class Northwind : DataContext { private static readonly string connectionString = "Data Source=WKS308\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;"; public Northwind() : base(connectionString) { } public Table<Customer> Customers { get { return this.GetTable<Customer>(); } } } [Table(Name = "Customers")] public class Customer { [Column()] public string CustomerID { get; set; } [Column()] public string CompanyName { get; set; } [Column()] public string ContactName { get; set; } [Column()] public string ContactTitle { get; set; } [Column()] public string Address { get; set; } [Column()] public string City { get; set; } [Column()] public string Region { get; set; } [Column()] public string PostalCode { get; set; } [Column()] public string Country { get; set; } [Column()] public string Phone { get; set; } [Column()] public string Fax { get; set; } } }
--DbContext sample -- this one error's out
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Linq; using System.Drawing; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Winfrm2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { NorthwindEntities northwind = new NorthwindEntities(); XDocument xml = new XDocument( new XComment("First 3 customers starting with 'C'"), new XElement("Customers", (from cust in northwind.Customers where cust.CompanyName.StartsWith("C") orderby cust.CompanyName select new XElement("Customer", new XElement("CustomerID", cust.CustomerID), new XElement("CompanyName", cust.CompanyName), new XElement("ContactTitle", cust.ContactTitle), new XElement("ContactName", cust.ContactName), new XElement("Address", cust.Address), new XElement("City", cust.City), new XElement("Region", cust.Region), new XElement("PostalCode", cust.PostalCode), new XElement("Country", cust.Country), new XElement("Phone", cust.Phone), new XElement("Fax", cust.Fax))).Take(3))); Console.WriteLine(xml.ToString()); } } } namespace Winfrm2 { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class NorthwindEntities : DbContext { public NorthwindEntities() : base("name=NorthwindEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<Customer> Customers { get; set; } } }--Error message from the DbContext sample
Thanks for any suggestions
Rich P