DataContext与实体类

// Define entity classes
[Table(Name = "CAR")]
public class CARclass
{
    [Column(Name = "Id")]
    public int Id { get; set; }
    [Column(Name = "Model")]
    public string Model { get; set; }
}

// Strongly typed DataContext
public partial class NorthWindDataContext : DataContext
{
    public Table<CARclass> CARs;
    public NorthWindDataContext(IDbConnection connection) : base(connection) { }
    public NorthWindDataContext(string connection) : base(connection) { }
}

static void Main(string[] args)
{
    NorthWindDataContext NWDC = new NorthWindDataContext(@"connection string");
    var NWCustomers = from NWs in NWDC.CARs
                        where NWs.Id == 1
                        select NWs;
    foreach (var cst in NWCustomers.ToList())
    {
        Console.WriteLine("Id = {0}, Model = {1}", cst.Id, cst.Model);
    }

    Console.ReadKey();
}
posted @ 2019-04-19 15:14  Kyle0418  阅读(356)  评论(0编辑  收藏  举报