Is this an example of LINQ-to-SQL?

2019-08-01 10:53发布

问题:

I made a little WPF application with a SQL CE database.

I built the following code with LINQ to get data out of the database, which was surprisingly easy. So I thought "this must be LINQ-to-SQL".

Then I did "add item" and added a "LINQ-to-SQL classes" .dbml file, dragged my table onto the Object Relational Designer but it said, "The selected object uses an unsupported data provider."

So then I questioned whether or not the following code actually is LINQ-to-SQL, since it indeed allows me to access data from my SQL CE database file, yet officially "LINQ-to-SQL" seems to be unsupported for SQL CE.

So is the following "LINQ-to-SQL" or not?

using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Windows;

namespace TestLinq22
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            MainDB db = new MainDB(@"Data Source=App_Data\Main.sdf");
            var customers = from c in db.Customers
                            select new {c.FirstName, c.LastName};
            TheListBox.ItemsSource = customers;
        }
    }

    [Database(Name = "MainDB")]
    public class MainDB : DataContext
    {
        public MainDB(string connection) : base(connection) { }
        public Table<Customers> Customers;
    }

    [Table(Name = "Customers")]
    public class Customers
    {
        [Column(DbType = "varchar(100)")]
        public string FirstName;

        [Column(DbType = "varchar(100)")]
        public string LastName;
    }

}

回答1:

This msdn article notes that while sdf is supported by the LINQ to SQL runtime, it is not supported by the designer.

To Quote:

  • The LINQ to SQL runtime and the SQLMetal command-line tool support SQL Server Compact 3.5.
  • The Object Relational Designer does not support SQL Server Compact 3.5.


回答2:

Yes, that's LINQ to SQL. It's using System.Data.Linq.DataContext, which is LINQ to SQL. It sounds like the designer doesn't support your provider, but LINQ to SQL itself does.

(Or the designer is being picky about your connection string, and would cope if you changed it a little bit. That sounds as likely as anything...)