2014年10月27日 星期一

Entity Framework Code First 對應規則設定

要改變預設對應規則,可以用兩種方式
一種是使用Attributes的方式,例如[key]
一種是覆寫DbContext的OnModelCreating,透過DbModelBuilder使用Fluent API方式來設定

資料表名稱和主索引
[Table("Table1")]
public class Table1
{
    [Key]
    public int id { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
    }
}

自動編號
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}
欄位名稱
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid")]
    public int id { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid");
    }
}

欄位型態
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
    }
}

不可為空(文字)
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }

    [Required]
    public string s1 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
        modelBuilder.Entity<Table1>().Property(x => x.s1)
            .IsRequired();
    }
}

最大長度(文字)
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }

    [Required]
    [MaxLength(10)]
    public string s1 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
        modelBuilder.Entity<Table1>().Property(x => x.s1)
            .IsRequired()
            .HasMaxLength(10);
    }
}

varchar, char(文字)
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }

    [Required]
    [MaxLength(10)]
    [Column(TypeName="char")]
    public string s1 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
        modelBuilder.Entity<Table1>().Property(x => x.s1)
            .IsRequired()
            .HasMaxLength(10)
            .IsFixedLength()
            .IsUnicode(false);
    }
}

精準度(浮點數)
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }

    [Required]
    [MaxLength(10)]
    [Column(TypeName="char")]
    public string s1 { get; set; }

    public decimal c1 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
        modelBuilder.Entity<Table1>().Property(x => x.s1)
            .IsRequired()
            .HasMaxLength(10)
            .IsFixedLength()
            .IsUnicode(false);
        modelBuilder.Entity<Table1>().Property(x => x.c1)
            .HasPrecision(18, 2);
    }
}

時間戳記
[Table("Table1")]
public class Table1
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("sid", TypeName = "bigint")]
    public int id { get; set; }

    [Required]
    [MaxLength(10)]
    [Column(TypeName = "char")]
    public string s1 { get; set; }

    public decimal c1 { get; set; }

    [Timestamp]
    public byte[] t1 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => x.id);
        modelBuilder.Entity<Table1>().Property(x => x.id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("sid")
            .HasColumnType("bigint");
        modelBuilder.Entity<Table1>().Property(x => x.s1)
            .IsRequired()
            .HasMaxLength(10)
            .IsFixedLength()
            .IsUnicode(false);
        modelBuilder.Entity<Table1>().Property(x => x.c1)
            .HasPrecision(18, 2);
        modelBuilder.Entity<Table1>().Property(x => x.t1)
            .IsRowVersion();
    }
}

複合索引
[Table("Table1")]
public class Table1
{
    [Key]
    [Column(Order = 1)]
    public int id1 { get; set; }

    [Key]
    [Column(Order = 2)]
    public int id2 { get; set; }
}

public class DemoContext : DbContext
{
    public DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>().ToTable("Table1").HasKey(x => new { x.id1, x.id2 });
    }
}