[Repost] Use Entity Framework Core to access the database (Oracle article) Use Entity Framework Core to access the database (Oracle article) Entity Framework Core database migration

Use Entity Framework Core to access the database (Oracle articles)

 
https://www.cnblogs.com/GuZhenYin/p/10756548.html

 

Foreword

Wow. . It ’s been a long time since I have been blogging for almost a year.

Recently, I have been busy with various home affairs and new frameworks of the company, and finally took the time to update a wave.

This article mainly talks about the mining pit of Entity Framework Core to access the oracle database. .

To emphasize, the Oracle official dll about Entity Framework Core accessing the oracle database has not been officially released before this article is published.

But I have used it in the project. . Mindful brothers can wait first. . Oracle is talking about the third quarter of this year. .

 

surroundings

1. Supported environments in official documentation

First let ’s take a look at the so-called official support.

operating system:

1. Windows x64
  1.1Windows 8.1 (Pro and Enterprise Editions)
  1.2Windows 10 x64 (Pro, Enterprise, and Education Editions)
  1.3Windows Server 2012 R2 x64 (Standard, Datacenter, Essentials, and FoundationEditions)
  1.4Windows Server 2016 x64 (Standard and Datacenter Editions)
2.Linux x64
  2.1Oracle Linux 7
  2.2Red Hat Enterprise Linux 7


.NET version:
  1.NET Core 2.1 or higher
  2.NET Framework 4.6.1 or higher


· Entity Framework Core version:
  1. 2.1 version or higher


Dependent libraries:
  1. ODP.NET Core 18.3 or higher
  2. Microsoft.EntityFrameworkCore.Relational 2.1 or higher
  3.Access to Oracle Database 11g Release 2 (11.2) or higher

 

text

 

This article will take the form of CodeFirst to create a database. .

1. Create a database

We create the context and entities as follows:

Copy code
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOracle(@"SQL Contion", b => b.UseOracleSQLCompatibility("11"));
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        //public int Rating { get; set; }
        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
Copy code

Here we first introduce the first point to note, the UseOracleSQLCompatibility method in the UseOracle parameter , the parameter passed in 11, refers to the oracle11g version. If you are 12g version please pass 12.

Because the SQL syntax of 11g and 12g has more differences, so use this to distinguish.

 

Then we add a version to execute the nuget command as follows: (PS: If you do n’t know how to use codeFirst, please move: Entity Framework Core database migration )

Add-Migration BanBen1

Then update the version to the database as follows:

Update-Database

The database is generated successfully.

 

2. Pit about oracle sequence

We now write the insert statement as follows:

using (BloggingContext db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "aaaaa1" });
                db.SaveChanges();
            }

Seemingly no problem statement, you will get an error message as follows:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

This is because we did not give the error message caused by the assignment of the primary key. (Because oracle does not increment the primary key, it can only increment by sequence)

So how to use auto-increment sequence?

We will find the database, as shown in the figure:

codefirst has generated the sequence for us, but it will not be used automatically. We need to configure:

Add the following code to the OnModelCreating method in the context:

Copy code
 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>(entity =>
            {
                entity.ToTable("Posts");
                entity.Property(o => o.PostId).ForOracleUseSequenceHiLo("Posts_PostId_sq3");

            });
            modelBuilder.Entity<Blog>(entity =>
            {
                entity.ToTable("Blogs");
                entity.Property(o => o.BlogId).ForOracleUseSequenceHiLo("Blogs_BlogId_sq1");

            });
        }
Copy code

Specify the sequence of the correspondence table.

Then running. You can add it successfully.

 

3. About the pits deployed in Docker

In my production project. It should be packaged into docker to run and deploy directly.

But in the process of packaging to docker, weird problems appeared again.

It will not be reproduced. . Anyway, there is no problem with the development environment. . It is no problem to put it directly in Linux. But once packaged to docker operation, the data will not be queried.

After many verifications, it was finally found that the rumtime mirror provided by Microsoft was a problem because of the reduced version system.

Add the following statement to the dockerfile to set the time zone when generating:

FROM microsoft/dotnet:2.1-aspnetcore-runtime
ENV TZ=Asia/Shanghai

This can successfully operate to the database. .

 

 

Conclusion

Recently, I have transplanted several projects to .NET CORE and encountered more or less pits. . It should be counted as countless pits. .

In fact, most of them are concentrated in the database connection. . For example, Oracle DB2. . (PS: I feel that mysql and sql server support is the best ...)

Although DB2 is officially released. But his pit is actually bigger than oracle. . We are writing next. .

Author: Gu India Source: http: //www.cnblogs.com/GuZhenYin/ If you think you have read this article to help, please click the "recommend" button, your "recommended" will be my greatest writing power! The copyright of this article belongs to the author and the blog garden. Welcome to reprint, but you must retain this statement without the author's consent
 
Category:  Entity Framework
Foreword

Wow. . It ’s been a long time since I have been blogging for almost a year.

Recently, I have been busy with various home affairs and new frameworks of the company, and finally took the time to update a wave.

This article mainly talks about the mining pit of Entity Framework Core to access the oracle database. .

To emphasize, the Oracle official dll about Entity Framework Core accessing the oracle database has not been officially released before this article is published.

But I have used it in the project. . Mindful brothers can wait first. . Oracle is talking about the third quarter of this year. .

 

surroundings

1. Supported environments in official documentation

First let ’s take a look at the so-called official support.

operating system:

1. Windows x64
  1.1Windows 8.1 (Pro and Enterprise Editions)
  1.2Windows 10 x64 (Pro, Enterprise, and Education Editions)
  1.3Windows Server 2012 R2 x64 (Standard, Datacenter, Essentials, and FoundationEditions)
  1.4Windows Server 2016 x64 (Standard and Datacenter Editions)
2.Linux x64
  2.1Oracle Linux 7
  2.2Red Hat Enterprise Linux 7


.NET version:
  1.NET Core 2.1 or higher
  2.NET Framework 4.6.1 or higher


· Entity Framework Core version:
  1. 2.1 version or higher


Dependent libraries:
  1. ODP.NET Core 18.3 or higher
  2. Microsoft.EntityFrameworkCore.Relational 2.1 or higher
  3.Access to Oracle Database 11g Release 2 (11.2) or higher

 

text

 

This article will take the form of CodeFirst to create a database. .

1. Create a database

We create the context and entities as follows:

Copy code
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseOracle(@"SQL Contion", b => b.UseOracleSQLCompatibility("11"));
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
        //public int Rating { get; set; }
        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
Copy code

Here we first introduce the first point to note, the UseOracleSQLCompatibility method in the UseOracle parameter , the parameter passed in 11, refers to the oracle11g version. If you are 12g version please pass 12.

Because the SQL syntax of 11g and 12g has more differences, so use this to distinguish.

 

Then we add a version to execute the nuget command as follows: (PS: If you do n’t know how to use codeFirst, please move: Entity Framework Core database migration )

Add-Migration BanBen1

Then update the version to the database as follows:

Update-Database

The database is generated successfully.

 

2. Pit about oracle sequence

We now write the insert statement as follows:

using (BloggingContext db = new BloggingContext())
            {
                db.Blogs.Add(new Blog { Url = "aaaaa1" });
                db.SaveChanges();
            }

Seemingly no problem statement, you will get an error message as follows:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

This is because we did not give the error message caused by the assignment of the primary key. (Because oracle does not increment the primary key, it can only increment by sequence)

So how to use auto-increment sequence?

We will find the database, as shown in the figure:

codefirst has generated the sequence for us, but it will not be used automatically. We need to configure:

Add the following code to the OnModelCreating method in the context:

Copy code
 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>(entity =>
            {
                entity.ToTable("Posts");
                entity.Property(o => o.PostId).ForOracleUseSequenceHiLo("Posts_PostId_sq3");

            });
            modelBuilder.Entity<Blog>(entity =>
            {
                entity.ToTable("Blogs");
                entity.Property(o => o.BlogId).ForOracleUseSequenceHiLo("Blogs_BlogId_sq1");

            });
        }
Copy code

Specify the sequence of the correspondence table.

Then running. You can add it successfully.

 

3. About the pits deployed in Docker

In my production project. It should be packaged into docker to run and deploy directly.

But in the process of packaging to docker, weird problems appeared again.

It will not be reproduced. . Anyway, there is no problem with the development environment. . It is no problem to put it directly in Linux. But once packaged to docker operation, the data will not be queried.

After many verifications, it was finally found that the rumtime mirror provided by Microsoft was a problem because of the reduced version system.

Add the following statement to the dockerfile to set the time zone when generating:

FROM microsoft/dotnet:2.1-aspnetcore-runtime
ENV TZ=Asia/Shanghai

This can successfully operate to the database. .

 

 

Conclusion

Recently, I have transplanted several projects to .NET CORE and encountered more or less pits. . It should be counted as countless pits. .

In fact, most of them are concentrated in the database connection. . For example, Oracle DB2. . (PS: I feel that mysql and sql server support is the best ...)

Although DB2 is officially released. But his pit is actually bigger than oracle. . We are writing next. .

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12698532.html