使用EF core的代码如下:using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace d0001_efc1
{
class Program
{
public class StudentContext : DbContext
{
public DbSet<Student>? Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseOracle(@"User Id=test_user;Password=123456;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=aaa)))");
}
}
[Table("STUDENT")]
public class Student
{
[Column("NUM")]
[Key]
public int num { get; set; }
[Column("NAME")]
public string? name { get; set; }
static void Main(string[] args)
{
using (var db = new StudentContext())
{
var student = new Student { num = 4, name = "张三" };
db.Students!.Add(student);
db.SaveChanges();
}
using (var db = new StudentContext())
{
var blogs = db.Students;
foreach (var item in blogs!)
{
Console.WriteLine(item.name);
Console.WriteLine(item.num);
//Console.WriteLine(item.Url + " has rating " + item.Rating );
}
}
Console.ReadLine();
}
}
}
}