摘要:1.实体类:
[Table(Name="Customers")]public class Customer{public string CustomerID;
public string City;}
使用Attribute定义ORM的关系,这样的好处是无需产生和维护大量映射文件。
[Table(Name="Customers")]public class Customer{[Column(Id=true)]public string CustomerID;
[Column]public string City;}
通过Attribute自定义列,定义主键(PK)……
2.数据上下文
// DataContext takes a connection string
DataContext db = new DataContext("c:\\northwind\\northwnd.mdf");
// Get a typed table to run queriesTable<Customer> Customers = db.GetTable<Customer>();
// Query for customers from Londonvar q =from c in Customerswhere c.City == "London"select c;
foreach (var cust in q)Console.WriteLine("id = {0}, City = {1}", cust.CustomerID, cust.City);
这个比较猛,直接写查询,from in where select都成了关键字。直接在对象上进行查询不再考虑数据库了。并且范型化了。
看到var,众位做过asp的兄弟是不是很亲切阿,这是动态语言(解释语言)的特性,变量定义时不定义类型,不过这里应该不是动态语言,应该是根据上下文在编译时确定类型的。
Northwind db = new Northwind("c:\\northwind\\northwnd.mdf");
var q =from c in db.Customerswhere c.City == "London"select c;
foreach (var cust in q) Console.WriteLine("id = {0}, City = {1}",cust.CustomerID, cust.City);
简单写法。
public partial class Northwind : DataContext{ public Table<Customer> Customers; public Table<Order> Orders;
public Northwind(string connection): base(connection) {}}
继承一下,表成了成员了。
3.关系定义
[Table(Name="Customers")]public class......[
阅读全文]