// 模型文件的路径,该文件由Entity Model Studio在生成源代码时同时生成
string
pathModel =
"..."
;
// 许可证文件路径,在完成试用版注册或者正式版许可证注册后许可证管理器
// 会自动生成该文件
pathLic =
// CustomModelSqlCS.ED是Entity Model Studio自动生成的类,其中的方法
// CreateContext也是自动生成的。用于完成EMContext对象的生成。
EmContext context = CustomModelSqlCS.ED.CreateContext(pathModel, pathLic);
// 构造Sql数据库的连接对象
EMSqlConnection conn =
new
EMSqlConnection(context);
// 构造Sql数据库的命令连接对象
EMSqlCommand comm =
EMSqlCommand(conn);
// 使用Eql接口完成查询语句对象的构造
EqlSelect stmt = Eql.Select(CustomModelSqlCS.ED.E7000).
From(CustomModelSqlCS.ED.E7000).
Where(CustomModelSqlCS.ED.E7000.ID > 10);
// 连接数据库。这里无需指明连接字符串,该字符串是在模型文件中配置的。
conn.Open();
// 使用Eql语句对象完成查询实体的ORM功能。参数EntityAffectType.Data用
// 于控制级联的深度。
ReadOnlyCollection<e7000> result = comm.QueryEntity<e7000>(stmt, EntityAffectType.Data);
// 关闭连接
conn.Cloes();
</e7000></e7000>
Company company =
Company();
company.Address =
"My Address"
company.CompanyName =
"WideUnion Tech"
comm.Save(company);
comm.Remove(company);
EqlDelete stmt = Eql.Delete(MyContext.Company).
From(MyContext.Company).
Where(MyContext.Company.ID == company.EntityId);
comm.Remove(stmt);
EqlSelect stmt = Eql.Select(MyContext.Company).
Where(MyContext.Company.ID > 10);
ReadOnlyCollection<company> company = comm.QueryEntity<company>(stmt);
</company></company>
// 实体方式
comm.SetupRelation(company.HasEmployee, employee);
// Eql方式
EqlSelect stmt = Eql.Select(MyContext.Employee).
From(MyContext.Employee).
Where(MyContext.Employee.ID == employee.EntityId);
comm.SetupRelation(company.HasEmployee, stmt);
comm.BreakRelation(company.HasEmployee, employee);
comm.BreakRelation(company.HasEmployee, stmt);
comm.TransitRelation(company1.HasEmployee, company2.HasEmployee, employee);
comm.TransitRelation(company1.HasEmployee, company2.HasEmployee, stmt);
// 使用参数EntityAffectType.Data表示只激活实体本身的数据,不执行任何级联操作。
ReadOnlyCollection<company> company = comm.QueryEntity<company>(stmt, EntityAffectType.Data);
// 这时访问HasEmployee属性就会执行懒加载
company[0].HasEmployee.Count.ToString();
// 使用参数EntityAffectType.Full表示执行所有的级联操作。那么QueryEntity
// 方法执行完,所有实体就都被激活了,所以懒加载失效。
ReadOnlyCollection<company> company = comm.QueryEntity<company>(stmt, EntityAffectType.Full);
// 这时访问HasEmployee属性不会执行懒加载
DbDataReader dr = comm.QueryDataReader(stmt);
DataSet ds = comm.QueryDataSet(stmt);