Create temp table with Entity Framework

Andre picture Andre · Jun 21, 2013 · Viewed 12.1k times · Source

I want to create a temp table in sql server by using Entity Framework. Is there any way I can do this? If I can create a temp table, my next question is, how I can read it?

Thanks in advance.

André

Answer

Andrew picture Andrew · Jun 21, 2013

Ok, so you don't like the stored procedures route....nor do I to be honest, but it's the quickest way I could think of doing it.

Based on that, I don't know of any easy way in EDM to create temp tables so my next suggestion would be to create local objects, which mimic the temporary table you wish to create. The name temporary obviously indicates you don't want them to live on the database indefinitely, so using in memory objects has the benefit of being much more controllable and (depending on your latency) quicker than making multiple calls to a database.

public class MyTempTable
{
    public string ID { get; set; }
    public string Column1 { get; set; }
    // your other columns here
}

List<MyTempTable> tempTable = new List<MyTempTable>();

Once you've created your list of objects you can do basically anything you'd normally do on the database table using Linq.