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";

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.

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,
    [CustomerID] ASC

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.

Other Articles of Interest

Next Article : How to amend a record using CodeFirst Entity Framework .NET
Previous Article : How to create a CodeFirst Entity Framework Project using Visual Studio

Tags - Entity Framework

Last Modified : 29th May 2023
Date Published : 13th August 2022

Comments and Questions

There's no register feature and no need to give an email address if you don't want to. All messages will be reviewed before being displayed. Comments may be merged or altered slightly, such as if it contains an email or website address.

You can decline to give a name; if that is the case, the comment will be attributed to a random star. A name is preferred, even if it's a random made-up one by yourself.

If you give an email address, you may receive an email notifying you when someone else has added a comment to the same page. In the email will be a link to unsubscribe to further notifications.


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