Bit of Technology

  • Archive
  • About Me
    • Advertise
    • Disclaimer
  • Speaking
  • Contact

Building ASP.Net Web API RESTful Service – Part 1

November 25, 2013 By Taiseer Joudeh 41 Comments

Be Sociable, Share!

  • Tweet
  • Email
  • WhatsApp

This is the first part of Building ASP.Net Web API RESTful Service Series. The topics we’ll cover are:

  • Building the Database Model using Entity Framework Code First – Part 1 (This Post).
  • Applying the Repository Pattern for the Data Access Layer – Part 2.
  • Getting started with ASP.Net Web API – Part 3.
  • Implement Model Factory, Dependency Injection and Configuring Formatters – Part 4.
  • Implement HTTP actions POST, PUT, and DELETE In Web API – Part 5.
  • Implement Resources Association – Part 6.
  • Implement Resources Pagination – Part 7.
  • Securing Web API – Part 8.
  • Preparing Web API for Versioning – Part 9.
  • Different techniques to Implement Versioning – Part 10.
  • Caching resources using CacheCow and ETag – Part 11.

Update (2014-March-5) Two new posts which cover ASP.Net Web API 2 new features:

  • ASP.NET Web API 2 Attribute Routing.
  • IHttpActionResult as new response type and CORS Support.

Update (2014-April-16) New multi part series tutorial which covers building OData Service using ASP.Net Web API.

  • Building OData Service using ASP.Net Web API.

Building the Database Model using Entity Framework Code First

We’ll be using Entity Framework “Code First approach” where we’ll define our model objects using “Plain Old CLR Objects” POCO. We’ll be code centeric and start by writing standard .NET classes which define the domain model objects that are suitable for our API. Those POCO classes will be responsible to generate our eLearning database.

The eLearning database is simple, we want to be able to define and store “Students”, and “Tutors”. As well we have to define and store “Courses” and “Subjects”. We need to allow each Student to enroll in different “Courses”.

The image below shows the final result of the database schema, I’m listing it early so it will facilitate the understanding of the POCO classes we’ll build now:

eLearning Database Schema

Step 1: Create a new empty Class Library Project

We’ll start by creating a new empty class library project which will be responsible of all data management operations (Data Layer). Choose File->New Project->Windows->Class Library and name your solution “eLearning” and your class library “Learning.Data”. You can choose .NET framework 4 or 4.5.

Step 2: Install Entity framework using NuGet

We need to install Entity framework version 5 or 6 using NuGet package manager or NuGet package console, the package we’ll install is named “EntityFramework“. Our solution will look as below after installing EntityFramework:

eLearningSolution

Step 3: Creating our Model

As we stated before, we do not have our eLearning database and we need to create it by writing standard .NET classes that define the domain model objects.

Now add a new folder called “Entities” then add five classes called “Subject”, “Course”, “Tutor”, “Student”, and “Enrollment” those classes contain just simple properties and will shape our database:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class Subject
    {
        public Subject()
        {
            Courses = new List<Course>();
        }
 
        public int Id { get; set; }
        public string Name { get; set; }
 
        public ICollection<Course> Courses;
    }
 
    public class Course
    {
        public Course()
        {
            Enrollments = new List<Enrollment>();
            CourseTutor = new Tutor();
            CourseSubject = new Subject();
        }
 
        public int Id { get; set; }
        public string Name { get; set; }
        public Double Duration { get; set; }
        public string Description { get; set; }
 
        public Tutor CourseTutor { get; set; }
        public Subject CourseSubject { get; set; }
 
        public ICollection<Enrollment> Enrollments { get; set; }
    }
 
    public class Tutor
    {
        public Tutor()
        {
            Courses = new List<Course>();
        }
        public int Id { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Enums.Gender Gender { get; set; }
 
        public ICollection<Course> Courses;
    }
 
    public class Student
    {
        public Student()
        {
            Enrollments = new List<Enrollment>();
        }
 
        public int Id { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Enums.Gender Gender { get; set; }
        public DateTime DateOfBirth { get; set; }
        public DateTime? RegistrationDate { get; set; }
        public DateTime? LastLoginDate { get; set; }
 
        public ICollection<Enrollment> Enrollments { get; set; }
    }
 
    public class Enrollment
    {
        public Enrollment()
        {
            Student = new Student();
            Course = new Course();
        }
        public int Id { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public Student Student { get; set; }
        public Course Course { get; set; }
    }

As you noticed those classes do not derive from any base classes nor have any attributes, having those standard classes give us more data access flexibility and allow us to focus on the application needs without worrying about persistence implementation.

Entity framework Code First by default supports an approach called “Convention over Configuration” for mapping your POCO classes to database objects (Tables, Table fields data types, and FK Relations). I find this approach is useful in scenarios where you are building a demo/simple applications. But in our case we need to override this conventions by providing custom database mapping rules using Fluent API.

Step 4: Applying Custom Mapping Rules

Once we apply the custom mapping rules we will be able to define datatype for each column, set null-ability, map FK relationships between tables, and specify PK and Identity columns.

To do this we need to create new folder named “Mappers” then add five classes which derives from System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>

Classes are: “CourseMapper”, “EnrollmentMapper”, “StudentMapper”, “SubjectMapper”, and “TutorMapper”.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    class CourseMapper : EntityTypeConfiguration<Course>
    {
        public CourseMapper()
        {
            this.ToTable("Courses");
 
            this.HasKey(c => c.Id);
            this.Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(c => c.Id).IsRequired();
 
            this.Property(c => c.Name).IsRequired();
            this.Property(c => c.Name).HasMaxLength(255);
 
            this.Property(c => c.Duration).IsRequired();
 
            this.Property(c => c.Description).IsOptional();
            this.Property(c => c.Description).HasMaxLength(1000);
 
            this.HasRequired(c => c.CourseSubject).WithMany().Map(s => s.MapKey("SubjectID"));
            this.HasRequired(c => c.CourseTutor).WithMany().Map(t => t.MapKey("TutorID"));
 
        }
    }
 
    class EnrollmentMapper : EntityTypeConfiguration<Enrollment>
    {
        public EnrollmentMapper()
        {
            this.ToTable("Enrollments");
 
            this.HasKey(e => e.Id);
            this.Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(e => e.Id).IsRequired();
 
            this.Property(e => e.EnrollmentDate).IsRequired();
            this.Property(e => e.EnrollmentDate).HasColumnType("smalldatetime");
 
            this.HasOptional(e => e.Student).WithMany(e => e.Enrollments).Map(s => s.MapKey("StudentID")).WillCascadeOnDelete(false);
            this.HasOptional(e => e.Course).WithMany(e => e.Enrollments).Map(c => c.MapKey("CourseID")).WillCascadeOnDelete(false);
        }
    }
 
    class StudentMapper : EntityTypeConfiguration<Student>
    {
        public StudentMapper()
        {
            this.ToTable("Students");
 
            this.HasKey(s => s.Id);
            this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(s => s.Id).IsRequired();
 
            this.Property(s => s.Email).IsRequired();
            this.Property(s => s.Email).HasMaxLength(255);
            this.Property(s => s.Email).IsUnicode(false);
 
            this.Property(s => s.UserName).IsRequired();
            this.Property(s => s.UserName).HasMaxLength(50);
            this.Property(s => s.UserName).IsUnicode(false);
 
            this.Property(s => s.Password).IsRequired();
            this.Property(s => s.Password).HasMaxLength(255);
 
            this.Property(s => s.FirstName).IsRequired();
            this.Property(s => s.FirstName).HasMaxLength(50);
 
            this.Property(s => s.LastName).IsRequired();
            this.Property(s => s.LastName).HasMaxLength(50);
 
            this.Property(s => s.Gender).IsOptional();
 
            this.Property(s => s.DateOfBirth).IsRequired();
            this.Property(s => s.DateOfBirth).HasColumnType("smalldatetime");
 
            this.Property(s => s.RegistrationDate).IsOptional();
            this.Property(s => s.RegistrationDate).HasColumnType("smalldatetime");
 
            this.Property(s => s.LastLoginDate).IsOptional();
            this.Property(s => s.LastLoginDate).HasColumnType("smalldatetime");
 
        }
    }
 
    class SubjectMapper : EntityTypeConfiguration<Subject>
    {
        public SubjectMapper()
        {
            this.ToTable("Subjects");
 
            this.HasKey(s => s.Id);
            this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(s => s.Id).IsRequired();
 
            this.Property(s => s.Name).IsRequired();
            this.Property(s => s.Name).HasMaxLength(255);
 
        }
    }
 
    class TutorMapper : EntityTypeConfiguration<Tutor>
    {
        public TutorMapper()
        {
            this.ToTable("Tutors");
 
            this.HasKey(s => s.Id);
            this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(s => s.Id).IsRequired();
 
            this.Property(s => s.Email).IsRequired();
            this.Property(s => s.Email).HasMaxLength(255);
            this.Property(s => s.Email).IsUnicode(false);
 
            this.Property(s => s.UserName).IsRequired();
            this.Property(s => s.UserName).HasMaxLength(50);
            this.Property(s => s.UserName).IsUnicode(false);
 
            this.Property(s => s.Password).IsRequired();
            this.Property(s => s.Password).HasMaxLength(255);
 
            this.Property(s => s.FirstName).IsRequired();
            this.Property(s => s.FirstName).HasMaxLength(50);
 
            this.Property(s => s.LastName).IsRequired();
            this.Property(s => s.LastName).HasMaxLength(50);
 
            this.Property(s => s.Gender).IsOptional();
        }
    }

By looking at the code above you will notice that we are configuring each POCO class property (Datatype, Null-ability, PK and identity columns, and FK relations). Those configuration will be reflected on the database tables we are building. For more details about mapping/configuring fluent API you can visit this link.

The relationships between eLearning database tables are simple and described as the below:

  • Each “Course” has a “Subject”.
  • Each “Tutor” can tech multiple “Courses”.
  • Each “Student” can enroll in multiple “Courses”. So we’ll have Many-to-Many table to persist the relation called “Enrollment”.

Step 5: Creating Context Class to Handle Database Persistence

Now we need to add new class named “LearningContext” which derives from class “System.Data.Entity.DbContext”:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class LearningContext : DbContext
    {
        public LearningContext() :
            base("eLearningConnection")
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;
 
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<LearningContext, LearningContextMigrationConfiguration>());
        }
 
        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Student> Students { get; set; }
        public DbSet<Subject> Subjects { get; set; }
        public DbSet<Tutor> Tutors { get; set; }
 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new StudentMapper());
            modelBuilder.Configurations.Add(new SubjectMapper());
            modelBuilder.Configurations.Add(new TutorMapper());
            modelBuilder.Configurations.Add(new CourseMapper());
            modelBuilder.Configurations.Add(new EnrollmentMapper());
 
            base.OnModelCreating(modelBuilder);
        }
    }

The “LearningContext” class is responsible for three tasks which they are:

  1. Exposing our POCO classes as public DbSet properties, this means that every POCO class is transferred to a database table.
  2. Overriding OnModelCreating procedure which is used to apply custom mapping rules for each POCO class by adding the new configurations to the DbModelBuilder configurations.
  3. In “LearningContext” class constructor we have implemented two things:
  • Disabled the ProxyCreationEnabled and LazyLoadingEnabled properties which they are enabled by default. The Lazy Loading property enables loading the sub-objects of model up front, in our case we want to load them on demand. The Proxy Creation property is used in conjugation with Lazy Loading property, so if is set to false the “LearningContext” won’t load sub-objects unless Include method is called.
  • Configured the initialization and migration strategy of the database to migrate to latest version if a model has changed (i.e. new property has been added). To implement this we need to add new class called “LearningContextMigrationConfiguration” which derives from class “System.Data.Entity.Migrations.DbMigrationsConfiguration<TContext>”. The code listing as below:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class LearningContextMigrationConfiguration : DbMigrationsConfiguration<LearningContext>
    {
        public LearningContextMigrationConfiguration()
        {
            this.AutomaticMigrationsEnabled = true;
            this.AutomaticMigrationDataLossAllowed = true;
 
        }
 
#if DEBUG
protected override void Seed(LearningContext context)
{
new LearningDataSeeder(context).Seed();
}
#endif
 
    }

The “LearningContextMigrationConfiguration” class is responsible for two tasks which they are:

  1. In the constructor of the class we set the property AutomaticMigrationsEnabled to true which means that we need EF to handle the automatic migration for us without caring about DB Versioning. As well we set the property AutomaticMigrationDataLossAllowed  to true, this is dangerous to set in production environment. If it was set to false an exception will be thrown if data loss may occur as part of an automatic migration, but for our series it is fine to keep it to true.
  2. Overriding the Seed procedure which is used to seed our database with initial data, this procedure gets called every time our application starts, I’ve created a class called “LearningDataSeeder” which responsible to seed the database, I won’t list it is code here but you can browse it on GitHub or by downloading the source code for the API.

Till this point we’ve implemented all the code needed to configure and create our eLearning database depending on model objects we’ve defined, we can stop at this point and consider our data layer completed, but we want to enhance it more and implement “Repository Pattern” which facilitates data access and manipulation once we start building the Web API. So in the next post we’ll implement the Repository Pattern for our data access layer.

Source code is available on GitHub.

Be Sociable, Share!

  • Tweet
  • Email
  • WhatsApp

Related Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Identity 2.1 Accounts Confirmation, and Password Policy Configuration – Part 2
  • ASP.NET Identity 2.1 with ASP.NET Web API 2.2 (Accounts Management) – Part 1

Filed Under: ASP.NET, ASP.Net Web API, CodeProject, Entity Framework, Web API Tutorial Tagged With: API, ASP.NET, Code First, Entity Framework, REST, RESTful, Tutorial, Web API, Web Service

Comments

  1. Avi says

    December 5, 2013 at 10:20 pm

    Hi,

    Kudos for your tutorial that has been very helpfull and a breeze to implement.
    Could you explain the difference between this 2 lines. I m struggling to understand why the WithMany is called once with parameters and once without:

    this.HasOptional(e => e.Student).WithMany(e => e.Enrollments).Map(s => s.MapKey(“StudentID”)).WillCascadeOnDelete(false);

    this.HasRequired(c => c.CourseTutor).WithMany().Map(t => t.MapKey(“TutorID”));

    What would be the impact on the database model created if i would use instead:
    this.HasRequired(c => c.CourseTutor).WithMany(t=>t.Courses).Map(t => t.MapKey(“TutorID”));

    Thaks

    Reply
    • tjoudeh says

      December 10, 2013 at 7:31 pm

      Hello Avi,
      Based on MSDN the HasRequired Configures a required relationship from this entity type. Instances of the entity type will not be able to be saved to the database unless this relationship is specified. The foreign key in the database will be non-nullable.

      Reply
      • Avi says

        December 10, 2013 at 7:36 pm

        hi,
        the question is more on the WithMany called once with a lambda and once without…

        Reply
  2. harris854 says

    January 8, 2014 at 9:28 am

    Hi tjoudeh,

    Nice work.:) Seems you missed stating about Enums class.

    namespace Learning.Data.Enums
    {
    public enum Gender
    {
    Male = 0,
    Female = 1
    }
    }

    Everything else is clearly explained for every beginners. This is exactly what I was searching.

    Thanks from my heart.:D

    Reply
    • tjoudeh says

      January 8, 2014 at 9:37 am

      Hello Harris,

      Glad it was beneficial for you. Good catch, I forgot to mention it, but it will function properly because it simple int enum.

      Reply
      • harris854 says

        January 10, 2014 at 7:04 am

        Yeah Cheers 🙂

        Reply
    • Stan says

      March 22, 2014 at 6:37 am

      Hey I’m fairly new to asp.net and I tried both of your was but the project wouldn’t build. The way I did it was…

      namespace Learning.Data.Entities
      {
      public class Enums
      {
      public enum Gender { male, female }
      }
      }

      hopefully this helps someone in the future. Thanks for the great tutorial

      Reply
      • Taiseer Joudeh says

        March 22, 2014 at 10:05 pm

        Thanks Stan for your feedback, maybe I missed this in the description, better to fork the running project on GitHub and follow along with your version. Good luck.

        Reply
  3. yonghan79 says

    January 10, 2014 at 5:52 am

    Hi,first of all thanks for the tutorial.I’ved downloaded your complete code,but when i run it,it gives

    Server Error in ‘/’ Application.
    The resource cannot be found

    How can i make it works?Thanks

    Reply
    • tjoudeh says

      January 10, 2014 at 9:08 am

      You welcome, there is no home page so thats normal, did you try to make Http Get request to one of the resource like http://localhost:yourport/api/courses/ using fiddler?

      Reply
      • yonghan79 says

        January 10, 2014 at 10:09 am

        I’m using firefox Poster addons,and it fails. It says : No response was received. Either the request could not be opened or the request timed out.

        Reply
      • tjoudeh says

        February 11, 2014 at 11:14 pm

        Sorry for the late reply but did it work with you using fiddler/postman?

        Reply
  4. Terje A. Bergesen (@tabinnorway) says

    March 6, 2014 at 10:30 am

    Simple question, why are you using the mapper classes, wouldn’t annotations serve the same purpose without the need for the additional classes? To me that would also improve readability. Is there any reason not to use them? In other words, drop the Mapping class and change the Courses class to look like this :

    [Table(“Courses”)]
    public class Course
    {
    [Key]
    public int Id { get; set; }

    [Required]
    [MaxLength(255),MinLength(5)]
    …
    public volatile ICollection Enrollments { get; set; }
    }

    Reply
    • Taiseer Joudeh says

      March 6, 2014 at 11:07 pm

      Hello Terje, you can use data annotations on entities, but I prefer to use fluent API and configure relationships in separate classes. In my opinion keeping your classes real Plain Old C Objects without any attributes is a good practice and you do all your configuration and mapping in one place.

      Reply
    • Md Tarif Hosen Sonet says

      April 22, 2014 at 10:14 am

      Hello Terje, Data annotations is subset of fluent API ,Everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true.Data annotations is good for simple application .good luck.

      Reply
      • Taiseer Joudeh says

        April 22, 2014 at 10:55 am

        Hello Md,

        You are right, Keeping your POCO classes a real POCOs with no attributes is good practice, that’s why I prefer using fluent API.

        Reply
  5. Mohamed Sadek says

    April 23, 2014 at 12:25 am

    Salem,
    First of all I would like to thank you very very much for this perfect tuto and for your serious work. Thank you sir.
    Now i am trying to rebuild this project but with “Code First Reverse Engineering existing Database”.
    So did I still need ” class LearningContextMigrationConfiguration ” ?

    Reply
    • Taiseer Joudeh says

      April 23, 2014 at 12:51 am

      Hello Mohamed, Glad you liked the tutorial, hopefully it will benefit you.

      You do not need to use ContextMigration class if you want, this class about setting your initialization strategy and will be helpful for future updates on your DB model (POCO classes).
      You can set it to Null or use another initialization strategy such as create data base if model doesn’t exist.

      Hope this answers your question.

      Reply
      • Mohamed Sadek says

        April 23, 2014 at 3:52 pm

        Thanks,
        This is all what I needed
        The good news that my solution until now work without erreur ( I am using my own existing DataBase)

        Can you please tell me where can I ask you about authentication Web Api V2 ( I am using VS 2012 )
        I have severe problem with it, maybe because all tuto that I found use VS 2013 and my Os don’t support it.

        Lots of regard
        May Allah bless you,

        Reply
        • Taiseer Joudeh says

          April 25, 2014 at 5:38 pm

          Hello Mohamed,
          It is not about Visual Studio version, it is about Framework you are using (.NET 4, 4.5, or 4.5.1) and the version of your Web API assemblies, so what you want to know about authentication?

          Reply
          • Mohamed Sadek says

            April 25, 2014 at 10:03 pm

            Hello,
            What I have to do is to create ” login authentication service web with WEP API V2.
            So how can I start, please.
            Any advices …

            and thank you a lot

  6. Jeremy Larcombe says

    May 20, 2014 at 11:00 am

    Thanks for a great tutorial, I’m just starting out with this technology and have found it difficult to find an end-to-end tutorial that just works. This is excellent and explains things with just the right amount of detail.

    One thing I can’t find is what I need to change to make this look at a different database. I can see EF has used my local SQLExpress instance by default as expected, but supposing the very same tables were in my production SQL Server 2008, how would I tell EF to look there instead?

    Many thanks!

    Reply
    • Taiseer Joudeh says

      May 20, 2014 at 1:21 pm

      Hello Jeremy,

      Glad you found this tutorial useful 🙂
      Just include a valid connection string in your web.config and it should point to the server/database in this connection and it won’t point to your SQL express, let me know if this works for you.

      Reply
      • Jeremy Larcombe says

        May 20, 2014 at 2:17 pm

        Perfect, thank you!

        Reply
  7. Yang Cao says

    August 21, 2014 at 8:45 am

    excellent ! thanks for sharing !

    Reply
    • Taiseer Joudeh says

      August 21, 2014 at 2:33 pm

      Glad you liked it 🙂

      Reply
  8. Josh Tomlinson says

    August 23, 2014 at 9:56 am

    Hello Taiseer. Thanks a bundle for this series, I love working with the way you injected the repository and model factory into a custom ApiController object that each controller could inherit from thus allowing easy access to these objects in a really easy way.

    I was wondering if you would consider adding to this series with a section on unit testing and how to set it up using your favorite testing and mocking tools. I would love to see how it all comes together.

    Reply
    • Taiseer Joudeh says

      August 23, 2014 at 6:21 pm

      Hi Josh,
      Thanks for taking the time to read the series and provide a feedback. I’m thinking to update this series to use Web API 2.2 and add the unit tests, it is one of the long to-do list items I’m planing to achieve :S hopefully I’ll have the time soon to work on this.

      Reply
      • Josh Tomlinson says

        August 23, 2014 at 11:44 pm

        That would be fantastic, I’ll keep checking back, now and then, to see what you end up deciding. Thanks for the reply!

        Reply
  9. WhyNot says

    October 25, 2014 at 9:31 pm

    Thanks for this fantastic set !

    I have a question though, I’m getting this Error :
    The type or namespace name ‘Enums’ could not be found (are you missing a using directive or an assembly reference?)
    any idea why ?

    Reply
    • Taiseer Joudeh says

      October 25, 2014 at 9:46 pm

      You are welcome, glad you liked it check this file on GitHub where I forgot to mention using it in the post. Simple Gender values for (0, and 1).
      Hope this answers your question.

      Reply
      • WhyNot says

        October 25, 2014 at 9:59 pm

        Great, solved my problem ! your site is a treasure that I just discovered and it seems that I’ll be here for a while 😉

        Reply
        • Taiseer Joudeh says

          October 27, 2014 at 10:45 am

          Thanks, happy to help. Let me know if you have another questions.

          Reply
  10. Engr umair shah says

    January 15, 2015 at 3:41 pm

    hi Taiseer nice tutorial could you please explain this code what you used in your model
    public Subject()
    {
    Courses = new List();
    }

    Reply
    • Taiseer Joudeh says

      January 15, 2015 at 8:44 pm

      Hi Engr, this is constructor is used to initialize the Courses list to avoid null exceptions, I want the property to contain an empty list of courses where I’ll fill later on demand.

      Reply
  11. rtipnisRT says

    June 5, 2015 at 1:44 am

    Wanted to mention one change I had to do with Entity Framework 6.1.3 Fluent API. I had to make the relationship as follows:
    this.HasRequired(i => i.ItemOrder)
    .WithMany(i => i.Items)
    .Map(o => o.MapKey(“OrderId”));

    If I didn’t include i => i.Items, EF ended up creating an extra (FK, nullable) column called Order_Id – which I didn’t want.

    Yes, I am using “independent association” exactly like the samples here.

    Reply
  12. Neeraj yadav says

    July 1, 2015 at 5:42 pm

    Hi Taiseer, another nice article I’m following to create architecture of my web api application. I decoupled authorization server and resource server. However both these servers point out same database and uses code first approach. Authorization server schema was created using aspnet identity and resource server I have my own schema.

    Is there any way to manage same database with code first model from two different applications. I heard about contextkey support in ef6 which can be used to do so. Is there any other best practice available.

    In my resource server I need AspNetUsers table to create foreign key relationship between user table and other tables. How should I do that, provided I don’t have AspNetUser POCO class on resource server.

    Reply
  13. Haitham Shaaban says

    December 2, 2015 at 12:57 pm

    hi TAISEER
    why no one discussed the the relation between user table and other tables every record added in any table “Admin” must know about it and also date of added and date of updated , i can not find this approach in any tutorial although it required in real world .

    Reply
  14. Rakesh says

    July 14, 2017 at 8:22 am

    hi Taiseer,

    Thanks for great article!
    i have question on models created from relational schema.
    there is one to may relationship between tutor and Courses.
    1 tutor can teach many courses –>which leads to –> tutor class will have list
    And you have also added Tutor object in Courses class.
    which means
    TutorObj–>List–>TutorObj
    However why do we need “Tutor Object” in Courses Class ? why not only “TutorId”
    i think there will be circular dependency with this design.
    Can you please explain if i am missing something

    Reply

Trackbacks

  1. Building ASP.Net Web API RESTful Service – Part 2 | Bit of Technology says:
    November 25, 2013 at 10:02 pm

    […] Building the Database Model using Entity Framework Code First – Part 1. […]

    Reply
  2. ASP.NET REST API szolgáltatás készítése | MolePlex says:
    February 11, 2014 at 2:22 pm

    […] ASP.NET REST API szolgáltatás készítése […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Taiseer

Husband, Father, Consultant @ MSFT, Life Time Learner... Read More…

Buy me a coffeeBuy me a coffee

Recent Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 – Part 5
  • ASP.NET Identity 2.1 Roles Based Authorization with ASP.NET Web API – Part 4

Blog Archives

Recent Posts

  • Integrate Azure AD B2C with ASP.NET MVC Web App – Part 3
  • Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
  • Azure Active Directory B2C Overview and Policies Management – Part 1
  • ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 – Part 5
  • ASP.NET Identity 2.1 Roles Based Authorization with ASP.NET Web API – Part 4

Tags

AJAX AngularJS API API Versioning ASP.NET Authentication Autherization Server Azure Active Directory B2C Azure AD B2C basic authentication C# CacheCow Client Side Templating Code First Dependency Injection Entity Framework ETag Foursquare API HTTP Caching HTTP Verbs IMDB API IoC Javascript jQuery JSON JSON Web Tokens JWT Model Factory Ninject OAuth OData Pagination Resources Association Resource Server REST RESTful Single Page Applications SPA Token Authentication Tutorial Web API Web API 2 Web API Security Web Service wordpress.com

Search

Copyright © 2022 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.