Entity Framework                
            コードの最初の規則
        
        
            
    サーチ…
備考
Conventionは、Code-Firstで作業するときに、ドメインクラス定義に基づいて概念モデルを自動的に構成するための一連のデフォルト規則です。コード - 最初の規則は、 System.Data.Entity.ModelConfiguration.Conventions名前空間( EF 5およびEF 6 )で定義されています。
主要な主要条約
クラスのプロパティの名前が "ID"(大文字と小文字は区別されません)またはクラス名の後に "ID"と指定されている場合、プロパティは主キーです。主キーのプロパティの型が数値またはGUIDの場合、ID列として構成されます。簡単な例:
public class Room
{ 
    // Primary key 
    public int RoomId{ get; set; } 
    ... 
}
条約の削除
 OnModelCreatingメソッドをオーバーライドすることによって、System.Data.Entity.ModelConfiguration.Conventions名前空間で定義されている規則を削除できます。 
次の例では、PluralizingTableNameConventionを削除します。
public class EshopContext : DbContext 
{ 
    public DbSet<Product> Products { set; get; }
    . . . 
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
    } 
}
デフォルトでは、EFはエンティティクラス名に 's'をつけたDBテーブルを作成します。この例では、コードファーストは代わりに、これPluralizingTableName規則を無視するように構成されてdbo.Productsテーブルdbo.Productテーブルが作成されます。 
タイプディスカバリー
デフォルトでコードファーストはモデルに含まれます
- コンテキストクラスのDbSetプロパティとして定義された型。
- 異なるアセンブリで定義されている場合でも、エンティティタイプに含まれる参照型。
- 基本クラスのみがDbSetプロパティとして定義されている場合でも派生クラス
ここでの例では、我々だけ追加していること、であるCompanyとしてDbSet<Company>当社のコンテキストクラスに: 
public class Company
{
    public int Id { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Department> Departments { set; get; }
}
public class Department
{
    public int Id { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Person> Staff { set; get; }
}
[Table("Staff")]
public class Person
{
    public int Id { set; get; }
    public string Name { set; get; }
    public decimal Salary { set; get; }
}
public class ProjectManager : Person
{
   public string ProjectManagerProperty { set; get; }
}
public class Developer : Person
{
    public string DeveloperProperty { set; get; }
}
public class Tester : Person
{
    public string TesterProperty { set; get; }
}    
public class ApplicationDbContext : DbContext
{
    public DbSet<Company> Companies { set; get; }
}
すべてのクラスがモデルに含まれていることがわかります
DecimalPropertyConvention
デフォルトでは、Entity Frameworkは小数点以下のプロパティをデータベーステーブルの10進(18,2)列にマップします。
public class Box
{
    public int Id { set; get; }
    public decimal Length { set; get; }
    public decimal Width { set; get; }
    public decimal Height { set; get; }
}
小数点以下のプロパティの精度を変更することができます。
1.流暢なAPIを使用する:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Box>().Property(b => b.Width).HasPrecision(20, 4);
}
「幅」プロパティのみが小数点(20,4)にマッピングされます。
2.規約を置き換える:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<DecimalPropertyConvention>();
    modelBuilder.Conventions.Add(new DecimalPropertyConvention(10, 4));
}
すべての10進数のプロパティは10進数(10,4)の列にマップされます。
関係条約
コードナビゲーションプロパティを使用して、2つのエンティティの関係を最初に推測します。このナビゲーションプロパティは、単純な参照型またはコレクション型にすることができます。例えば、StudentクラスのStandardナビゲーションプロパティとStandardクラスのICollectionナビゲーションプロパティを定義しました。したがって、Code Firstは、Standard_StandardId外部キー列をStudentsテーブルに挿入することにより、StandardsとStudents DBテーブルの間に一対多の関係を自動的に作成しました。
public class Student
{
    
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }      
        
    //Navigation property
    public Standard Standard { get; set; }
}
public class Standard
{
   
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    //Collection navigation property
    public IList<Student> Students { get; set; }
   
}
上記のエンティティは、Standard_StandardId外部キーを使用して次の関係を作成しました。
外部キー協定
クラスAがクラスBとの関係にあり、クラスBがAの主キーと同じ名前とタイプのプロパティを持つ場合、EFは自動的にそのプロパティを外部キーとみなします。
public class Department
{
    public int DepartmentId { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Person> Staff { set; get; }
}
public class Person
{
    public int Id { set; get; }
    public string Name { set; get; }
    public decimal Salary { set; get; }
    public int DepartmentId { set; get; }
    public virtual Department Department { set; get; }
}
この場合、DepartmentIdは明示的な指定がない外部キーです。




