用EF的三种方式(SqlServer数据库和Oracle数据库)

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改的,获取的更新,