Quantcast
Channel: Sampath Lokuge Tech Blog
Viewing all articles
Browse latest Browse all 107

Entity Framework Fluent API Article's Sample Code

$
0
0
Related Article : How to Use Entity Framework Fluent API ?

Project Tree

Project Tree


SchoolEntities.cs


using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace EFFluventAPI.Models
{
  publicclassSchoolEntities: DbContext
  {
    publicDbSet<Course> Courses { get; set; }
    publicDbSet<Department> Departments { get; set; }
    publicDbSet<Instructor> Instructors { get; set; }
    publicDbSet<OfficeAssignment> OfficeAssignments { get; set; }

    protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder)
    {
      //Configure Code First to ignore PluralizingTableName convention
      //If you keep this convention then the generated tables
      //will have pluralized names
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

      modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorId);

      modelBuilder.Entity<Department>()
            .HasKey(t => new { t.DepartmentId, t.Name });

      modelBuilder.Entity<Department>()
            .Property(t => t.DepartmentId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

      modelBuilder.Entity<Department>().Property(t => t.Name).HasMaxLength(50);

      modelBuilder.Entity<Department>().Property(t => t.Name).IsRequired();

      modelBuilder.Entity<Department>().Ignore(t => t.Budget);

      modelBuilder.Entity<Department>()
             .Property(t => t.Name).HasColumnName("DepartmentName");

      modelBuilder.Entity<Course>()
             .HasRequired(c => c.Department)
             .WithMany(t => t.Courses).Map(m => m.MapKey("ChangedDepartmentId"));

      modelBuilder.Entity<Department>().Property(t => t.Name).IsUnicode(false);

      modelBuilder.Entity<Department>()
            .Property(p => p.Name).HasColumnType("varchar");

      modelBuilder.ComplexType<Details>()
            .Property(t => t.Location).HasMaxLength(20);

      modelBuilder.Entity<OnsiteCourse>()
           .Property(t => t.Details.Location).HasMaxLength(20);

      modelBuilder.Entity<OfficeAssignment>()
             .Property(t => t.Timestamp).IsConcurrencyToken(); 
            
       modelBuilder.Entity<OfficeAssignment>()
            .Property(t => t.Timestamp).IsRowVersion();

        }
    }
}



Course.cs

using System.Collections.Generic;

namespace EFFluventAPI.Models
{
    publicclassCourse
    {
        publicCourse()
        {
            this.Instructors = newHashSet<Instructor>();
        }

        // Primary key
        publicint CourseId { get; set; }

        publicstring Title { get; set; }
        publicint Credits { get; set; }

        // Foreign key
        publicint DepartmentId { get; set; }

        // Navigation properties
        publicvirtualDepartmentDepartment { get; set; }
        publicvirtualICollection<Instructor> Instructors { get; privateset; }
    }
}

Department.cs

using System.Collections.Generic;

namespace EFFluventAPI.Models
{
    publicclassDepartment
    {
        publicDepartment()
        {
            this.Courses = newHashSet<Course>();
        }

        // Primary key
        publicint DepartmentId { get; set; }
        publicstring Name { get; set; }
        publicdecimal Budget { get; set; }
        publicSystem.DateTime StartDate { get; set; }
        publicint? Administrator { get; set; }

        // Navigation property
        publicvirtualICollection<Course> Courses { get; privateset; }
    }
}

Instructor.cs

using System;
using System.Collections.Generic;

namespace EFFluventAPI.Models
{
    publicclassInstructor
    {
        publicInstructor()
        {
            this.Courses = newList<Course>();
        }

        // Primary key
        publicint InstructorId { get; set; }
        publicstring LastName { get; set; }
        publicstring FirstName { get; set; }
        publicDateTime HireDate { get; set; }

        // Navigation properties
        publicvirtualICollection<Course> Courses { get; privateset; }
    }
}


OfficeAssignment.cs

using System;
using System.ComponentModel.DataAnnotations;

namespace EFFluventAPI.Models
{
    publicclassOfficeAssignment
    {
        publicInt32 InstructorId { get; set; }

        publicstring Location { get; set; }

        publicByte[] Timestamp { get; set; }

        // Navigation property
        publicvirtualInstructorInstructor { get; set; }
    }
}

Details.cs


using System;

namespace EFFluventAPI.Models
{
    publicclassDetails
    {
        publicDateTime Time { get; set; }
        publicstring Location { get; set; }
        publicstring Days { get; set; }
    }
}

OnsiteCourse.cs

namespace EFFluventAPI.Models
{
    publicclassOnsiteCourse: Course
    {
        publicOnsiteCourse()
        {
            Details = newDetails();
        }

        publicDetails Details { get; set; }
    }
}

Related Article : How to Use Entity Framework Fluent API ?

Viewing all articles
Browse latest Browse all 107

Trending Articles