ComponentOne AppView for ASP.NET WebForms
Tutorials / Creating an Event Planning Application / Step 3 of 4: Creating the Models
In This Topic
    Step 3 of 4: Creating the Models
    In This Topic

    In this step, you'll create the Models used in your application.

    1. From the Models folder, double-click the Event.cs file to open it.
      1. Check your references and make sure that they resemble the following:

        To write code in C#

        C#
        Copy Code
        using System;
        using System.Collections.Generic;
        using System.Data.Entity;
        using System.Linq;
        using System.Web;
      2. Add the following code below the namespace declaration:

        To write code in C#

        C#
        Copy Code
        public class EventObj
            {
                [Key]
                public int Id { get; set; }
                public string Subject { get; set; }
                public string Location { get; set; }
                public DateTime Start { get; set; }
                public DateTime End { get; set; }
                public string Description { get; set; }
                public bool AllDay { get; set; }
            }
    2. From the Models folder again, double-click the EventAction.cs file to open it.
      1. Check your references and make sure that they resemble the following:

        To write code in C#

        C#
        Copy Code
        using System;
        using System.Collections.Generic;
        using System.Data;
        using System.Linq;
        using System.Web;
      2. Add the following code below the namespace declaration:

        To write code in C#

        C#
        Copy Code
        public static class EventAction
            {
                private static EventPlannerEntities _eventDb = new EventPlannerEntities();
         
                internal static EventPlannerEntities GetEventDb()
                {
                    return new EventPlannerEntities();
                }
         
                public static IList<EventObj> GetEvents()
                {
                    return _eventDb.Events.ToList();
                }
         
                public static EventObj GetEventDetail(int id)
                {
                    return _eventDb.Events.Find(id);
                }
         
                public static EventObj Create()
                {
                    return new EventObj
                    {
                        Subject = "New event",
                        Start = DateTime.Today,
                        End = DateTime.Today.AddDays(1).AddSeconds(-1),
                        AllDay = false
                    };
                }
         
                public static void Add(EventObj eventObj)
                {
                    _eventDb.Events.Add(eventObj);
                    _eventDb.SaveChanges();
                }
         
                public static void Edit(EventObj eventObj)
                {
                    _eventDb.Entry(eventObj).State = EntityState.Modified;
                    _eventDb.SaveChanges();
                }
         
                public static void Delete(int id)
                {
                    EventObj eventObj = _eventDb.Events.Find(id);
                    _eventDb.Events.Remove(eventObj);
                    _eventDb.SaveChanges();
                }
            }
        }
    3. Double-click the last code file in the Models folder to open the EventPlannerEntities.cs file.
      1. Check the references and make sure that they resemble the following:

        To write code in C#

        C#
        Copy Code
        using System;
        using System.Collections.Generic;
        using System.Data.Entity;
        using System.Linq;
        using System.Web;
      2. Add the following code  below the namespace declaration:

        To write code in C#

        C#
        Copy Code
        public class EventPlannerEntities : DbContext
            {
                public DbSet<EventObj> Events { get; set; }
         
                public EventPlannerEntities()
                {
                    Database.CreateIfNotExists();
                }
            }
        }