Tampilkan postingan dengan label framework. Tampilkan semua postingan
Tampilkan postingan dengan label framework. Tampilkan semua postingan

Entity Framework Fluent API Articles Sample Code

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
{
  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; }

    protectedoverride voidOnModelCreating(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
    {
        publicCourse()
        {
            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 DepartmentDepartment { get; set; }
        public virtual ICollection<Instructor> Instructors { get; private set; }
    }
}

Department.cs

using System.Collections.Generic;

namespace EFFluventAPI.Models
{
    public class Department
    {
        publicDepartment()
        {
            this.Courses = new HashSet<Course>();
        }

        // Primary key
        public int DepartmentId { get; set; }
        public string Name { get; set; }
        public decimal Budget { get; set; }
        publicSystem.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
    {
        publicInstructor()
        {
            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 InstructorInstructor { 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; }
    }
}

OnsiteCourse.cs

namespace EFFluventAPI.Models
{
    public class OnsiteCourse: Course
    {
        publicOnsiteCourse()
        {
            Details = newDetails();
        }

        public Details Details { get; set; }
    }
}

Related Article : How to Use Entity Framework Fluent API ?
Read More..

Spring Framework Tutorial How to call Stored Procedures from Java using IN and OUT parameter example

Spring Framework provides excellent support to call stored procedures from Java application. In fact there are multiple ways to call stored procedure in Spring Framework, e.g. you can use one of the query() method from JdbcTemplate to call stored procedures, or you can extend abstract class StoredProcedure to call stored procedures from Java. In this Java Spring tutorial, we will see second approach to call stored procedure. It's more object oriented, but same time requires more coding. StoredProcedure class allows you to declare IN and OUT parameters and call stored procedure using its various execute() method, which has protected access and can only be called from sub class. I personally prefer to implement StoredProcedure class as Inner class, if its tied up with one of DAO Object, e.g. in this case it nicely fit inside EmployeeDAO. Then you can provide convenient method to wrap stored procedure calls. In order to demonstrate, how to call stored procedures from spring based application, we will first create a simple stored proc using MySQL database, as shown below.
Read more »
Read More..