Project Tree
SchoolEntities.cs
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace EFFluventAPI.Models
{
public class SchoolEntities
: DbContext
{
public DbSet<Course>
Courses { get; set;
}
public DbSet<Department>
Departments { get; set;
}
public DbSet<Instructor>
Instructors { get; set;
}
public DbSet<OfficeAssignment>
OfficeAssignments { get; set; }
protected
override void
OnModelCreating(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
{
public class Course
{
public
Course()
{
this.Instructors
= new HashSet<Instructor>();
}
// Primary
key
public int CourseId { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
// Foreign
key
public int DepartmentId { get;
set; }
// Navigation
properties
public virtual Department
Department { get; set;
}
public virtual ICollection<Instructor> Instructors { get; private set; }
}
}
using System.Collections.Generic;
namespace EFFluventAPI.Models
{
public class Department
{
public
Department()
{
this.Courses
= new HashSet<Course>();
}
// Primary
key
public int DepartmentId { get;
set; }
public string Name { get; set; }
public decimal Budget { get;
set; }
public
System.DateTime StartDate { get; set; }
public int? Administrator { get;
set; }
// Navigation
property
public virtual ICollection<Course> Courses { get;
private set; }
}
}
Instructor.cs
using System;
using System.Collections.Generic;
namespace EFFluventAPI.Models
{
public class Instructor
{
public
Instructor()
{
this.Courses
= new List<Course>();
}
// Primary
key
public int InstructorId { get;
set; }
public string LastName { get;
set; }
public string FirstName { get;
set; }
public DateTime HireDate { get;
set; }
// Navigation
properties
public virtual ICollection<Course> Courses { get;
private set; }
}
}
OfficeAssignment.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace EFFluventAPI.Models
{
public class OfficeAssignment
{
public Int32 InstructorId { get;
set; }
public string Location { get;
set; }
public Byte[] Timestamp { get;
set; }
// Navigation
property
public virtual Instructor
Instructor { get; set;
}
}
}
Details.cs
using System;
namespace EFFluventAPI.Models
{
public class Details
{
public DateTime Time { get;
set; }
public string Location { get;
set; }
public string Days { get; set; }
}
}
namespace EFFluventAPI.Models
{
public class OnsiteCourse
: Course
{
public
OnsiteCourse()
{
Details = new
Details();
}
public Details Details { get;
set; }
}
}
Related Article : How to Use Entity Framework Fluent API ?
Sampath
ReplyDeleteThis is even better than Entity Framework:
https://www.kellermansoftware.com/p-47-net-data-access-layer.aspx