记.net core 6 集成efcore7 oracle

1、安装nuget包Oracle.EntityFrameworkCore、Microsoft.EntityFrameworkCore

2、增加表对应关系以及初始化方法

/// 
/// ef操作
/// 
public class EFDBForOracle:DbContext
{
    public DbSet tests {  get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseOracle("User ID=user;Password=pwd;Data Source=127.0.0.1/dbname");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity();
    }
}

/// 
/// ef实体
/// 
[Table(name: "BASE_ALIBABA_CARD_URL")]
//[PrimaryKey(nameof(base_alibaba_card_url_id))]
public class Test
{
    [Key]
    [Required(AllowEmptyStrings = false)]//非空
    [Column("BASE_ALIBABA_CARD_URL_ID")]
    [StringLength(10)]
    [Display(Name = "主键")]
    public int base_alibaba_card_url_id { get; set; }

    [Required(AllowEmptyStrings = false)]//非空
    [Column("APPID")]
    [StringLength(32)]
    [Display(Name = "appid")]
    public string appid {  get; set; }

    [Required(AllowEmptyStrings = false)]//非空
    [Column("TEMPLATE_ID")]
    [StringLength(32)]
    public string template_id {  get; set; }

    [Required(AllowEmptyStrings = false)]//非空
    [Column("APPLY_CARD_URL")]
    [StringLength(20)]
    public string apply_card_url {  get; set; }
}

3、使用

/// 
/// 查询
/// 
/// 
[HttpPost("search")]
public string Search()
{
    EFDBForOracle eFDBForOracle = new EFDBForOracle();

    //查
    List tests = eFDBForOracle.tests.ToList();

    //改
    tests[0].template_id = "测试修改";

    //增
    eFDBForOracle.tests.Add(new Test()
    {
        base_alibaba_card_url_id = DateTime.Now.Millisecond,
        appid = "测",
        template_id = "模板",
        apply_card_url = "url"
    });
    
    IQueryable tests2 = eFDBForOracle.tests.Where(s => s.appid == "2");
    //Test test3 = new Test()
    //{
    //    base_alibaba_card_url_id= 1,
    //    template_id = "测试修改",
    //    apply_card_url = "测试修改",
    //    appid = "2"
    //};
    //eFDBForOracle.tests.Attach(test3);
    //操作保存
    eFDBForOracle.SaveChanges();

    return tests.Count.ToString();
}

你可能感兴趣的:(.netcore)