Code First ile Fluent Api kullanma

Öncelikle modelimizi ve Modelimize saracak DbContext classlarımızı oluşturalım..

public class SchoolEntities : DbContext
{
  #region Properties
 
  public DbSet<Course> Courses { get; set; }
  public DbSet<Department> Departments { get; set; }
 
  #endregion
 
  #region Methods
 
  protected override void OnModelCreating(
    System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);
 
    // put here the fluent code
  }
 
  #endregion
}
 
public partial class Course
{
  #region Properties
 
  public int CourseID { get; set; }
  public string Title { get; set; }
  public string Days { get; set; }
  public DateTime Time { get; set; }
  public string Location { get; set; }
  public int Credits { get; set; }
  public int DepartmentID { get; set; }
 
  public virtual Department Department { get; set; }
 
  #endregion
}
 
public class Department
{
  #region Properties
 
  public int DepartmentID { get; set; }
  public string Name { get; set; }
  public decimal Budget { get; set; }
  public DateTime StartDate { get; set; }
  public int Administrator { get; set; }
 
  public virtual ICollection<Course> Courses { get; set; }
 
  #endregion
}

 

 

 

Daha sonra Fluent Api kullanarak modelimizde düzenlemeler yapalım. Fluen Api kullanmadan Data annotions kullaran tabii bazı düzenlemerler yapabilrisiniz, fakat herşeyi değil.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
 
  modelBuilder.Entity<Department>().
    Property(d => d.Name).
    IsRequired().
    HasMaxLength(50);
 
  modelBuilder.Entity<Department>().
    Property(d => d.DepartmentID).
    HasDatabaseGenerationOption(DatabaseGenerationOption.None);
 
  modelBuilder.Entity<Department>().
    HasMany(d => d.Courses).
    WithRequired(c => c.Department).
    HasForeignKey(c => c.DepartmentID).
    WillCascadeOnDelete();
 
  modelBuilder.Entity<Department>().
    Ignore(d => d.Administrator);
 
  modelBuilder.Entity<Course>().
    Property(c => c.Title).
    IsRequired().
    HasColumnName("Name");      
}

 

Daha sonra modelimize bilgi ekleyelim ve sonucu görelim...

class Program
{
  static void Main(string[] args)
  {
    using (SchoolEntities context = new SchoolEntities())
    {
      var department = new Department
      {
        DepartmentID = 1,
        Administrator = 2,
        Budget = 100000,
        Name = "Data Access",
        StartDate = DateTime.Now
      };
      var course = new Course
      {
        Credits = 2,
        Days = "MF",
        Location = "Class 1",
        Time = DateTime.Now,
        Title = "Entity Framework",
        Department = department,
      };
      context.Departments.Add(department);
      context.SaveChanges();
    }
  }
}


Eğer veri tabanımızın ismini değiştirmek sitoyrsak DbContext ile sardığımız sınıfımızın temel kurucu yönteminde (base constructor) belirtebilriiz.

public SchoolEntities() :
      base("MySchool")
    {
 
    }

Veri tabanımız aşağıdaki gibi bir görüntü alacaktır.


Ayrıntılı bilgi içinburdan takip edebilrisiniz.
Buradan
ve buradan


 

Fluent Api bir yazim seklidir. Method tarzinda LinQ ifadesi yazmak gibi. Bunu yazim tarzinin mucidi Martin Fowler dir.  Buradan ayrintili bir bilgi alabilirsin. Burada Fluent Api LinQ to Entity veya LinqQ to SQL de kod yazarken kullanilan bir yontem dir.burada da ayrintili bir bilgi var.

The file '/Custom/Widgets/Calendar/widget.cshtml' does not exist.The file '/Custom/Widgets/Category list/widget.cshtml' does not exist.The file '/Custom/Widgets/Tag cloud/widget.cshtml' does not exist.The file '/Custom/Widgets/Page List/widget.cshtml' does not exist.The file '/Custom/Widgets/Month List/widget.cshtml' does not exist.