先看看这2个类
class Customer {
public int Id { get; set; }
...
}
class CustomerAddress {
public int Id { get; set; }
public int CustomerId { get; set; } // Reference based on Property name convention
}
---
也支持 别名
[Alias("LegacyCustomer")]
class Customer {
public int Id { get; set; }
...
}
class CustomerAddress {
public int Id { get; set; }
[Alias("LegacyCustomerId")] // Matches `LegacyCustomer` Alias
public int RenamedCustomerId { get; set; } // Reference based on Alias Convention
1对1 直接引用CustomerAddress 爽了很多
public class Customer
{
...
public int CustomerAddressId { get; set; }
[Reference]
public CustomerAddress PrimaryAddress { get; set; }
外键和引用属性
public class Customer
{
[References(typeof(CustomerAddress))]
public int PrimaryAddressId { get; set; }
[Reference]
public CustomerAddress PrimaryAddress { get; set; }
多搞几个外键
public class Customer
{
[AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
[References(typeof(CustomerAddress))]
public int? HomeAddressId { get; set; }
[References(typeof(CustomerAddress))]
public int? WorkAddressId { get; set; }
[Reference]
public CustomerAddress HomeAddress { get; set; }
[Reference]
public CustomerAddress WorkAddress { get; set; }
}
这是官网的例子 建议执行后到数据库里看看,跟踪下数据和语句
ar customer = new Customer
{
Name = "The Customer",
HomeAddress = new CustomerAddress {
Address = "1 Home Street",
Country = "US"
},
WorkAddress = new CustomerAddress {
Address = "2 Work Road",
Country = "UK"
},
};
db.Save(customer, references:true);
var c = db.LoadSelect (q => q.Name == "The Customer");
c.WorkAddress.Address.Print(); // 2 Work Road
var ukAddress = db.Single (q => q.Country == "UK");
ukAddress.Address.Print(); // 2 Work Road
join
var dbCustomers = db.Select (q => q.Join ());//注意表的关联 尤其是id字段(表CustomerAddress有一个名为CustomerId的字段,对应Customer表的id字段)
SELECT Customer.* FROM Customer
INNER JOIN
CustomerAddress ON (Customer.Id == CustomerAddress.Id)
还可以
SqlExpression q = db.From ();
q.Join ((cust,address) => cust.Id == address.CustomerId);
List dbCustomers = db.Select(q);
也可以这样写(不写条件,隐式的关联), 不过最好还是显示的把条件写出来。
q.Join ();
q.Join ();
q.Join ((cust,address) => cust.Id == address.CustomerId);
多表的多列
List customers=db.Select < FullCustomerInfo > (db.From< Customer >().Join < CustomerAddress > ());
等同
var customers = db.Select < FullCustomerInfo,Customer > (q = > q.Join < CustomerAddress > ());
Customer是表名 Id是字段名 下面的四个知道是什么意思了吧
CustomerId => "Customer"."Id"
OrderId => "Order"."Id"
CustomerName => "Customer"."Name"
OrderCost => "Order"."Cost"
群people乱舞
List rows = db.Select ( // 结果匹配FullCustomerInfo
db.From () // 起始 Customer
.LeftJoin () // 左联接CustomerAddress 没加条件哦 隐式的关联了
.Join ((c,o) => c.Id == o.CustomerId) // join链接 order 这个加了条件 on
.Where(c => c.Name == "Customer 1") // 基本表Customer的条件
.And (o => o.Cost < 2) // 对 Order加条件
.Or ((c,o) => c.Name == o.LineItem)); // 用的是or呦 关联条件 是where里的条件
还是要看看这个 左联接 有链接什么的
https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs
https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/LoadReferencesJoinTests.cs
是否保存引用的表记录
var customer = new Customer {
Name = "Customer 1",
PrimaryAddress = new CustomerAddress {
AddressLine1 = "1 Australia Street",
Country = "Australia"
},
Orders = new[] {
new Order { LineItem = "Line 1", Qty = 1, Cost = 1.99m },
new Order { LineItem = "Line 2", Qty = 2, Cost = 2.99m },
}.ToList(),
};
db.Save(customer, references:true);
Load* 通过id加载一条记录 并且加载关联的子表的记录
var customer = db.LoadSingleById (customerId);
下面的这句也能明了的吧。select和SingleById的区别
var customers = db.LoadSelect (q => q.Name == "Customer 1");
更多 请看https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs
---
Merge 的使用 合并不连贯的结果集合
List customers = db.Select (q =>
q.Join ()
.Where (o => o.Qty >= 10)
.SelectDistinct());
上面的是客户集合
List orders = db.Select (o => o.Qty >= 10);
上面的是订单集合
customers.Merge(orders); 这句用来合体
customers.PrintDump(); 打印下或者查询下结果。订单被关联到客户了。 孤儿得到救助。
注:没测试,关联不到的,可能被抛弃了?。
没试过, 隐式的,万一是凤姐咋办
var customerWithAddress = db.LoadSingleById (customer.Id, include: new[] { "PrimaryAddress" });
版本号
public class Poco
{
...
public ulong RowVersion { get; set; }
}
sqlserver 是RowVersion 类型
Uses PostgreSql's xmin system column (no column on table required)
Uses UPDATE triggers on MySql, Sqlite and Oracle whose lifetime is attached to Create/Drop tables APIs
蛋疼 就是加一列好了
db.DeleteById (id:updatedRow.Id, rowversion:updatedRow.RowVersion)
判断版本号
更新和删除试试 看看版本号的变化
var rowId = db.Insert(new Poco { Text = "Text" }, selectIdentity:true);
var row = db.SingleById (rowId);
row.Text += " Updated";
db.Update(row); //success!
row.Text += "Attempting to update stale record";
//Can't update stale record
Assert.Throws (() =>
db.Update(row));
//Can update latest version
var updatedRow = db.SingleById (rowId); // fresh version
updatedRow.Text += "Update Success!";
db.Update(updatedRow);
updatedRow = db.SingleById (rowId);
db.Delete(updatedRow);
手洗
List Select (string sql, IEnumerable sqlParams)
T Single (string sql, IEnumerable sqlParams)
T Scalar (string sql, IEnumerable sqlParams)
List Column (string sql, IEnumerable sqlParams)
IEnumerable ColumnLazy (string sql, IEnumerable sqlParams)
HashSet ColumnDistinct (string sql, IEnumerable sqlParams)
Dictionary<K, List > Lookup (string sql, IEnumerable sqlParams)
List SqlList (string sql, IEnumerable sqlParams)
List SqlColumn (string sql, IEnumerable sqlParams)
T SqlScalar (string sql, IEnumerable sqlParams)
IDbDataParameter pAge = db.CreateParam("age", 40, dbType:DbType.Int16);
db.Select ("SELECT * FROM Person WHERE Age > @pAge", new[] { pAge });