Step by step instructions on how to convert Excel spreadsheet to Entity Framework solution
Tags: net, c, nerdspeak
Published: 2021-10-16 02:12:00

---
title: Step by step instructions on how to convert Excel spreadsheet to Entity Framework solution
published: true
date: 2016-10-09 12:12:00 UTC
tags: NET,c,nerdSpeak
canonical_url: https://waiholiu.blogspot.com/2016/10/excel2EF.html
---

Sometimes you have some Excel data and all you want to do is quickly analyse it. This article details how to quickly import the data into an SQL database and get an entity framework model mapped to it so that you can quickly mess with the data however you like.

  

For this example, I have a spreadsheet of all the AFL matches since 2009 and their respective odds. I got this data from this link (http://www.aussportsbetting.com/data/historical-afl-results-and-odds-data/). 

  

Depending on the excel spreadsheet, you might need to clean it up a little bit. In the case of this dataset, I had to delete the first row because it was an extra header that was confusing my import tool.

  

1.       Firstly, import data into a sql table using the SQL import data tool (if table doesn’t have a primary key, you’ll need to create one)

  

a. Open up SQL Server and create a new database. Mine will be called AFLOdds.

  

b. Right click properties and select Import Data

  

[![](https://2.bp.blogspot.com/-fClD5pQa_As/V_oxpKx927I/AAAAAAAApxg/vD4QSOiBaKwmIS81Q5n0Bk5lDOjYAJ0WACLcB/s640/pic1.png)](https://2.bp.blogspot.com/-fClD5pQa_As/V_oxpKx927I/AAAAAAAApxg/vD4QSOiBaKwmIS81Q5n0Bk5lDOjYAJ0WACLcB/s1600/pic1.png)

c. In Choose a data source, select the Excel file

d. In Choose a Destination, find your Database

e. In Specify Table Copy or Query – Choose Copy data from one or more tables or views

f. Choose the sheet that you would like to copy

g. After going through the process, you should successfully see that a table is created. Check that there is data inside the table.

  
  

 ![](https://1.bp.blogspot.com/-oAC00jBJt_0/V_ox5yEkxjI/AAAAAAAApxk/GvB6vcMwTsw75dYw3Cm5hGaxOZmt4T6OgCLcB/s1600/pic2.png)

  

  

h. (Optional) You may need to actually add a primary key.

  

  

[![](https://2.bp.blogspot.com/-Xw2JrGQiNzQ/V_oyTl5zrvI/AAAAAAAApxs/IQ1ILPjJ6Q005rHRBqvG4sRilH6-5rqOwCLcB/s1600/pic3.png)](https://2.bp.blogspot.com/-Xw2JrGQiNzQ/V_oyTl5zrvI/AAAAAAAApxs/IQ1ILPjJ6Q005rHRBqvG4sRilH6-5rqOwCLcB/s1600/pic3.png)

  
  

2.       Create new visual studio solution 

  

3.       Add entity framework from nuget

[![](https://4.bp.blogspot.com/-SPpXc7tekH8/V_oym3l-KLI/AAAAAAAApxw/ervbOH0TidIud3E0M8ua9hGr2A-5V5NPwCLcB/s1600/pic4.png)](https://4.bp.blogspot.com/-SPpXc7tekH8/V_oym3l-KLI/AAAAAAAApxw/ervbOH0TidIud3E0M8ua9hGr2A-5V5NPwCLcB/s1600/pic4.png)

  

  

  

4.       Add a new file -> ADO entity data object

  

[![](https://2.bp.blogspot.com/-YQimEQeensM/V_ozNAO6flI/AAAAAAAApx4/LhnxEXe2SrkvMpgKtOd_eRmid6SMkSUEACLcB/s1600/pic5.png)](https://2.bp.blogspot.com/-YQimEQeensM/V_ozNAO6flI/AAAAAAAApx4/LhnxEXe2SrkvMpgKtOd_eRmid6SMkSUEACLcB/s1600/pic5.png)

  

  

5.       Select Code First from database

  

[![](https://1.bp.blogspot.com/-HYws9Ke20Z8/V_ozW8NouCI/AAAAAAAApyA/nAO6EV9baHEsoHrLcxmgrviwWk9f7KNdwCLcB/s1600/pic6.png)](https://1.bp.blogspot.com/-HYws9Ke20Z8/V_ozW8NouCI/AAAAAAAApyA/nAO6EV9baHEsoHrLcxmgrviwWk9f7KNdwCLcB/s1600/pic6.png)

  

Note – if it’s not appearing, you may need to install it http://stackoverflow.com/questions/22647674/code-first-from-database-template-not-showing-in-visual-studio-entity-data-mod

  

6.       Find your database and select all tables and views you want. Models should be generated

  

[![](https://3.bp.blogspot.com/-xm6JkMsCKjQ/V_ozl0xV1HI/AAAAAAAApyI/Nus3Z0Dkmfw3re7QWqTlN13QRPFP8I8QACLcB/s1600/pic7.png)](https://3.bp.blogspot.com/-xm6JkMsCKjQ/V_ozl0xV1HI/AAAAAAAApyI/Nus3Z0Dkmfw3re7QWqTlN13QRPFP8I8QACLcB/s1600/pic7.png)
  

[![](https://1.bp.blogspot.com/-XEorM9meiM8/V_ozmh92DMI/AAAAAAAApyM/jHhki9ftuH8bsV5OhSQEO3zdk-G0FzXxQCLcB/s1600/pic8.png)](https://1.bp.blogspot.com/-XEorM9meiM8/V_ozmh92DMI/AAAAAAAApyM/jHhki9ftuH8bsV5OhSQEO3zdk-G0FzXxQCLcB/s1600/pic8.png)

  

7.Your solution can now connect to the database.  You can paste something like below to test that it works. 

  

static void Main(string[] args)

        {

  

            using (var db = new Model1())

            {

  

                var datums = db.Data.ToList();

  

                foreach (var rows in datums)

                {

  

                    Console.WriteLine(String.Format("{0} vs {1}", rows.Home\_Team, rows.Away\_Team));

                }

            }

  

            Console.ReadLine();

  

        }

  

Now you can use entity framework and the power of LINQ queries to analyse the data however you like.