EF CodeFirst 入门 for oracle
创建单表插入
1.安装
设定DB连接
<connectionStrings> <add name="connstr" providerName="Oracle.ManagedDataAccess.Client" connectionString="Password=XXX;User ID=XXXX;Data Source=//10.1.1.1:1521/XXXX;" /> </connectionStrings>
2.创建实体类
public class STUDENT { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int STUDENTID { get; set; } public string STUDENTNAME { get; set; } public DateTime? DATEOFBIRTH { get; set; } public string PHOTO { get; set; } public float HEIGHT { get; set; } public float WEIGHT { get; set; } }
3.实现DBContext
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Text; using System.Threading.Tasks; using Oracle.ManagedDataAccess.EntityFramework; namespace EFDemo { public class SchoolContext:DbContext { public SchoolContext():base("name=connstr") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("FPCSFCDATA");//指定默认用户 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); // 映射的表移除复数,去除 s //如果这个不加,那么保存数据的时候会在类名后面加小写的s,然后在数据库生成这个表
} public DbSet<STUDENT> Students { get; set; } } }
4.添加数据
STUDENT stu = new STUDENT() { STUDENTID = 3, STUDENTNAME = "李四", HEIGHT = 180.5F, WEIGHT = 140.5F, DATEOFBIRTH = DateTime.Now, PHOTO = "987654321"};
dc.Students.Add(stu);
dc.SaveChanges();
MessageBox.Show("创建成功");