记.net core 6 集成efcore7 oracle

发布时间:2024年01月18日

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

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

/// <summary>
/// ef操作
/// </summary>
public class EFDBForOracle:DbContext
{
    public DbSet<Test> 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<Test>();
    }
}

/// <summary>
/// ef实体
/// </summary>
[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、使用

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

    //查
    List<Test> 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<Test> 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();
}

文章来源:https://blog.csdn.net/qq_40934195/article/details/135674368
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。