Code First

Entity Framework Code First 資料庫初始化設定

在參考了entityFramework套件之後,設定檔中會自動增加一個EntityFramework的區域設定 其中的defaultConnectionFactory宣告了預設的資料庫位置 不同的版本預設的localdb實體名稱會有不同,這邊的例子是mssqllocaldb <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 就會自動建立一個包含完整Assembly名稱的資料庫 [![](http://3.bp.blogspot.com/-u6i3p3OnimM/VFNEtuySOlI/AAAAAAAABtk/lDxVN9zSfD8/s1600/db.png)](http://3.bp.blogspot.com/-u6i3p3OnimM/VFNEtuySOlI/AAAAAAAABtk/lDxVN9zSfD8/s1600/db.png) 如果在設定檔的連線區段中,有一個和DbContext同名的設定值,就會改用這邊的連線設定 建立的資料庫也會是連線字串中指定的名稱 <connectionStrings> <add name="DemoContext" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Demo;Integrated Security=true;"/> </connectionStrings>[![](http://2.bp.blogspot.com/-B1iIDWV-srg/VFNGwAUTTXI/AAAAAAAABt4/eI2Gmbx2deY/s1600/02.%E9%80%A3%E7%B7%9A%E5%AD%97%E4%B8%B2.png)](http://2.bp.blogspot.com/-B1iIDWV-srg/VFNGwAUTTXI/AAAAAAAABt4/eI2Gmbx2deY/s1600/02.%E9%80%A3%E7%B7%9A%E5%AD%97%E4%B8%B2.png) 也可以透過預設建構式指定要使用設定檔中的那個值 <connectionStrings> <add name="


Entity Framework Code First 預設對應規則

資料庫 預設採用SQLEXPRESS,如果沒有SQLEXPRESS執行個體,會嘗試Localdb 如果有和DbContext同名的連線字串,就會直接使用 也可透過覆寫建構式的時後,傳入指定的連線字串名稱 或是透過DbConnection,在執行階段傳入連線物件 Table 資料表為類別名稱的複數形式,而且schema都為dbo 可以透過System.ComponentModel.DataAnnotations.Schema.Table 或是Fluent API的Entity<T>.ToTable來指定資料表的名稱 也可以覆寫OnModelCreating,移除複數資料表的約定 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 主索引 預設為類別名稱加上Id結尾的屬性,如果資料形態是int,還會加上自動編號 可以透過System.ComponentModel.DataAnnotations.Key 或是Fluent API的Entity<T>.HasKey來指定 如果是複合索引,Attribute方式需再加上Column來指定欄位的順序 Fluent API則是傳入一個匿名型別 外部索引 如果類別之間有一對多的關系,會自動生成Foreign Key 欄位名稱是導覽屬性加上底線再加上對應主索引的欄位名稱 可以透過System.ComponentModel.DataAnnotations.Schema.ForeignKey來指定 或是透過Fluent API的Entity<T>.Hasxxx().Withxxx來指定 xxx可以是Optional(0或多個)、Required(1或多個)、Many(多個) 資料型態 實值型別會是not null 參考型別會是null nullable型別會是null 可以透過System.ComponentModel.DataAnnotations.Required來設定不可空值 或是透過Fluent API的Entity<T>().IsRequired()來指定不可空值,IsOptional指定可以空值 也可以透過System.ComponentModel.DataAnnotations.Schema.Column來明確指定對應的格式 或是透過Fluent API的Entity<T>().Property().HasColumnType來指定對應的格式 或是IsUnicode來指定是否為Unicode C#SQL ServernullablebytetinyintNOT NULLshortsmallintNOT NULLintintNOT NULLlongbigintNOT NULLfloatrealNOT NULLdoublefloatNOT NULLdecimaldecimal(18,2)NOT NULLstringnvarchar(max)NULLbyte[]varbinary(max)NOT NULLboolbitNOT NULLdatetimedatetimeNOT NULL