0

I have looked around and found ways of clearing a table using TRUNCATE, this however only works with tables that are not being referenced as a foreign key.

I have also found a method which describes finding the range of all the data, and then deleting that range. This is however a somewhat less efficient way, and would like to avoid it if there is a better alternative.

What would be the most efficient way to empty these 2 tables?

public class Product
{
    public int ProductID { get; set; }
    public String ProductNaam { get; set; }

    [ForeignKey("Afdeling")]
    public int? AfdelingID { get; set; }
    public virtual Afdeling Afdeling { get; set; }
}

public class Afdeling
{
    public int AfdelingId { get; set; }
    public string Naam { get; set; }
}

I am using Visual Studio 2013 Ultimate in a .net 4.5 WPF project, with Entity Framework 6.1.2 and LocalDB

SpiritBH
  • 329
  • 3
  • 13

1 Answers1

1

Duplicate of : Entity Framework. Delete all rows in table

Check the community wiki answer.

public static void Clear<T>(this DbSet<T> dbSet) where T : class
{
    dbSet.RemoveRange(dbSet);
}

Then

DbContext.Afedlings.Clear();
DbContext.Products.Clear();
await VotingTestContext.SaveChangesAsync();

Of course assuming that you can keep track of relationships. Cheers.

Community
  • 1
  • 1
Darek
  • 4,687
  • 31
  • 47