Delete all entities in Entity Framework

后端 未结 8 1701
天命终不由人
天命终不由人 2020-12-13 17:10

I want to delete content of all tables (all entities) using Entity Framework 4+. How can this be done?

8条回答
  •  不知归路
    2020-12-13 17:44

    Iterate through the tables with a code something like this:

    context.GetType().GetProperties()
    .Where(propertyInfo => propertyInfo.PropertyType == typeof(Table<>))
    .Select(propertyInfo => propertyInfo.GetValue(context, null) as ITable).ToList()
    .Foreach(table =>
    {
        //code that deletes the actual tables records.
    }
    );
    

提交回复
热议问题