NBear v2.2.1+ 支援強型別實體查詢語法,以運算子重載實現編譯期檢查、直觀易讀、跨資料庫平台的查詢表達。
LocalUser[] users = gateway.Select<LocalUser>(
_Entity.LocalUser.Id > 5 | _Entity.LocalUser.LoginId == "teddy",
_Entity.LocalUser.Id.Desc & _Entity.LocalUser.LoginId.Asc
);
等同傳統字串 SQL:
gateway.Select<LocalUser>("[Id] > @Id or [LoginId] = @LoginId", "[Id] desc, [LoginId]", new object[] { 5, "teddy" });
使用 Entity Configurator 載入組件 → 設置元數據 → 「Code → Generate EntityQuery Code」→ 複製生成代碼至專案。產出 _Entity 命名空間含所有實體的 PropertyItem 靜態屬性。
& (And)、| (Or)、! (Not)==、!=、>、<、>=、<=.Like("pattern")& (ThenBy)namespace _Entity {
public class LocalUser {
public static PropertyItem Password = new PropertyItem("Password", "[", "]", "@");
public static PropertyItem LoginId = new PropertyItem("LoginId", "[", "]", "@");
public static PropertyItem Id = new PropertyItem("Id", "[", "]", "@");
public static PropertyItem Name = new PropertyItem("Name", "[", "]", "@");
public static PropertyItem PrivilegeOwnerId = new PropertyItem("PrivilegeOwnerId", "[", "]", "@");
}
// ... 其他實體同理
}
此機制大幅提升查詢代碼的可維護性與重構安全性。