文档介绍:NET 基础篇—— Entity Framework 数据转换层通用类
2012CSDN 博客之星火热出炉! 2013 年全国百
所高校巡讲讲师招募
st<T>(List<T>
entityList) where T : EntityObject
2
{
3
int changedCount = 0;
4
try
5
{
6
using (BasicArchitectureEntities
context = new BasicArchitectureEntities())
7
{
8
foreach (T entity in
entityList)
9
(typeof(T).Name, entity);
10 changedCount = ();
11 if (changedCount > 0)
12
();
13
}
14
}
15
catch (Exception ex)
16
{ ....... }
17
return changedCount;
18
}
2. Delete
同样地, ObjectContext 类当中也存在方法
( object entity )用于删除实体。
首先通过输入的参数 id 建立起 EntityKey 对象,然后在
ObjectContext 查找此实体,若实体存在则使用
( object entity )方法把此实体删
除 。
1
public int Delete<T>(int id) where T :
EntityObject
2
{
3
int changedCount = 0;
4
try
5
{
6
using (BasicArchitectureEntities
context = new BasicArchitectureEntities())
7
{
8
//建立 EntityKey
对象
9
EntityKey entityKey = new
EntityKey(
10
"BasicArchitectureEntities." + typeof(T).Name, "Id", id);
11
//通过 EntityKey
找到实体
12
var objResult =
(entityKey);
13
//若实体存在则删除实体
14
if (objResult != null)
15
(objResult);
16 changedCount =
();
17
if (changedCount > 0)
18
();
19
}
20
}
21
catch (Exception ex)
22
{ ...... }
23
return changedCount;
24
}
( object entity )与
( entitySetName string ,object entity )
相同,可以通过导航属性,一次性删除多个关联实体。但如
果数据库中存在下面的数据
Company 表:
Position 表:
此时使用此 int Delete<Company> (2) 方法删除
Company 对象,系统将会报错。这是由于导航属性在默认情
况下具