Example application using .NET Entity Framework

Hopefully, you still have the sample application you created in the previous article, link below. We're now going to build upon it to create a sample application. The first thing is to create classes that will map directly to database tables in SQL Server.

Create the following classes in the application. They can be in the same file or separate files.

    class Customer

    {
        public int CustomerID;
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string County { get; set; }
        public string Postcode { get; set; }
        public ICollection<Address> Addresses { get; set; }
    }

    class Address
    {
        public int ID;
        public string Line1 { get; set; }
        public string Line2 { get; set; }
        public string town { get; set; }
        public string county { get; set; }
        public string postcode { get; set; }
    }

In addition to those classes above, we will create a Database class for the store which will do the database operations, as you can see below. One thing I should point out, in the Shop class file, you need to add a "using" command so that DbContest can be understood.

    using System.Data.Entity;

        class shop : DbContext
        {
            public shop(string sCon) : base(sCon) {}
            public DbSet<Address> Addresses { get; set; }
            public DbSet<Customer> Customers { get; set; }
        }

sCon is the connection string to your database. I've left that blank so you can pass in your method for getting the connection string. If you need help with the connection string, I can recommend my article How to get a Connection String from Web.Config or App.Config in C# for help.

We'll now look at a sample .NET C# application to create the tables and save a record.

    static void Main(string[] args)
    {
        string sConn = "Data Source=;Initial Catalog=Shop;Integrated Security=True";

        using (var ctx = new Shop(sConn))
        {
            Customer oCust = new Customer();
            oCust.FirstName = "Sean";
            oCust.Surname = "Connery";
            ctx.Customers.Add(oCust);
            ctx.SaveChanges();
        }
    }

When you run, the tables will be created in SQL Server, and you can use the management studio to see the tables. You will notice there are no Stored Procedures. It doesn't create any.

Screen is too small to see picture, please view on a screen that is at least 380 pixels wide.

You can see from the picture it has assumed the ID field will be the primary key for the table. All the string fields have been created as nvarchar(max) as it doesn't know how big the field will be. We can look at changing that later. The equivalent SQL to create the table is:-

CREATE TABLE [dbo].[Customers](
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](max) NULL,
    [Surname] [nvarchar](max) NULL,
    [Line1] [nvarchar](max) NULL,
    [Line2] [nvarchar](max) NULL,
    [County] [nvarchar](max) NULL,
    [Postcode] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.Customers] PRIMARY KEY CLUSTERED
(
    [CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
    ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

If you run the code multiple times, it will create the same record repeatedly. It will not update the record. You have been warned. The next article will show you how to update an object.

Tags - Entity Framework

Last Modified : June 2023


About... / Contact.. / Cookie...