SqlServer數據庫數據庫
1.DB Firstapp
現有DB,生成edmx文件ide
貼一下生成的model測試
//------------------------------------------------------------------------------ // <auto-generated> // 此代碼已從模板生成。 // // 手動更改此文件可能致使應用程序出現意外的行爲。 // 若是從新生成代碼,將覆蓋對此文件的手動更改。 // </auto-generated> //------------------------------------------------------------------------------ namespace Ruanmou.EFDBFirst { using System; using System.Collections.Generic; public partial class JD_Commodity_001 { public int Id { get; set; } public Nullable<long> ProductId { get; set; } public Nullable<int> CategoryId { get; set; } public string Title { get; set; } public Nullable<decimal> Price { get; set; } public string Url { get; set; } public string ImageUrl { get; set; } } }
2.Code Firstui
有數據庫,從數據庫得到model,就是這個spa
貼一下生成的Model,和DB First的不太同樣,長度attribute加上了code
[Table("JD_Commodity_001")]//1 特性 public partial class JDCommodity001 { [Key] public int Id { get; set; } public long? ProductId { get; set; } //[ForeignKey] [Column("CategoryId")] public int? ClassId { get; set; } [StringLength(500)] public string Title { get; set; } public decimal? Price { get; set; } [StringLength(1000)] public string Url { get; set; } [StringLength(1000)] public string ImageUrl { get; set; } }
若是數據庫字段或表名和model的不同(好比想去掉下劃線)能夠有3種方式,方式1見上圖,Model上或屬性上加attributeblog
方式2在 OnModelCreating 裏添加映射,code first 的 OnModelCreating 和DB first的不同, db 的什麼也沒寫,截圖下code first的OnModelCreating ci
代碼,啓動時能夠完成數據庫和代碼結構的同步部署
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //啓動時能夠完成數據庫和代碼結構的同步 //new CreateDatabaseIfNotExists<codeFirstDbContext>();//默認 不存在就建立 //new DropCreateDatabaseAlways<codeFirstDbContext>();//每次都刪除重建 //new DropCreateDatabaseIfModelChanges<codeFirstDbContext>(); //Database.SetInitializer<codeFirstDbContext>(new DropCreateDatabaseIfModelChanges<codeFirstDbContext>()); //對不起 數據都沒了。。 測試/快速部署 其實這裏還能夠完成數據初始化 //請必定當心 modelBuilder.Entity<JDCommodity002>() .ToTable("JD_Commodity_002") .Property(c => c.ClassId) .HasColumnName("CategoryId");//2 鏈式API modelBuilder.Configurations.Add(new JDCommodity003Mapping());//3 映射文件 modelBuilder.Entity<Category>() .Property(e => e.Code) .IsUnicode(false);
方式3 Mapping 的方式,寫個Mapping 文件,上面的代碼有,聽說沒什麼人用
Oracle數據庫
可能引用這個就能夠用吧
DB first , OnModelCreating 什麼也沒寫 model仍是沒有長度的attribute
protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); }
這個是EF5
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
模型管理器管理外鍵,由於名字不同看着不開心,全改爲FK 開頭的了用Powerdesigner改的,獲取的更新,