C/C++教程

EF CodeFirst 入门 for oracle

本文主要是介绍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("创建成功");

  

  

  

 

这篇关于EF CodeFirst 入门 for oracle的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!