In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to configure EF Core to operate Oracle". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to configure EF Core to operate Oracle".
Configuration
To use EF Core to manipulate the Oracle database, you first need to install Oracle.EntityFrameworkCore. You can search for the installation directly on NuGet.
By convention, creating a Context class inherits DbContext is no different from other databases, but when we configure the connection string, we need to specify the version of the Oracle database for it, passing "11" for 11g and "12" for 12c.
Public class MyContext: DbContext {public MyContext (DbContextOptions options): base (options) {} protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) {optionsBuilder.UseOracle ("DATA SOURCE=LOCALHOST:1521/ORACLE1;USER ID= your ID; PASSWORD= your password;", b = > b.UseOracleSQLCompatibility ("12")); / / specify database version}}
If we want EF Core to output the executed Sql during debugging, I use the method to customize a log class to output the Sql to Console.
And register it at configuration time.
1 protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) 2 {3 / configure database connection 4 optionsBuilder.UseOracle (5 "DATA SOURCE=LOCALHOST:1521/ORACLE1;USER ID= your ID; PASSWORD= your password;" 6, b = > b.UseOracleSQLCompatibility ("12")); 7 / configure Sql output console 8 var loggerFactory = new LoggerFactory (); 9 loggerFactory.AddProvider (new EFLoggerProvider ()); 10 optionsBuilder.UseLoggerFactory (loggerFactory); 11 12 base.OnConfiguring (optionsBuilder) 13}
Since we are demonstrating Demo, we typically use dependency injection to register when developing ASP.NET Core projects. If you use dependency injection, you can write it this way.
Services.AddDomainContext (builder = > {/ / configure database connection builder.UseOracle ("DATA SOURCE=LOCALHOST:1521/ORACLE1;USER ID= your ID; PASSWORD= your password;", b = > b.UseOracleSQLCompatibility (version)); / / configure Sql output console var loggerFactory = new LoggerFactory (); loggerFactory.AddProvider (new EFLoggerProvider ()); builder.UseLoggerFactory (loggerFactory);}); query data
When executing a query, there are two points to pay attention to. One is that the attribute name of the entity class mapped by Table needs to be in full uppercase if it is not explicitly specified, otherwise the error "ORA-00904: invalid identifier" will be reported. Because all the field names are decorated in double quotation marks in the generated Sql, the default of the field names stored in Oracle is in all uppercase.
So if we don't want to capitalize all the attribute names of the entity class, we need to explicitly specify the name of the mapped column. You can use the properties of DataAnnotations to annotate all attributes, or you can specify column mappings in Context. In addition, if you need to specify Schema when executing the query, configure it as follows.
Method 1:
Protected override void OnModelCreating (ModelBuilder modelBuilder) {/ / determine whether the current database is Oracle if (this.Database.IsOracle ()) {/ / add the Schema name manually if necessary. If it is the default or the Schema name is not required in front of the table, you do not need to configure modelBuilder.HasDefaultSchema ("XUHY");} modelBuilder.Entity (entity = > {entity.ToTable ("EMPLOYEE")) Entity.Property (e = > e.Id). IsRequired (); 12 / / column mapping entity.Property (e = > e.Id) .HasColumnName ("ID"); entity.Property (e = > e.BirthDay) .HasColumnName ("BIRTHDAY"); entity.Property (e = > e.Department) .HasColumnName ("DEPARTMENT"); entity.Property (e = > e.EmployeeNo) .HasColumnName ("EMPLOYEENO") Entity.Property (e = > e.IsValid) .HasColumnName ("ISVALID"); entity.Property (e = > e.Name) .HasColumnName ("NAME");}); base.OnModelCreating (modelBuilder);}
Method 2:
[Table ("EMPLOYEE")] / / specify database corresponding table name public class Employee {[Key] / primary key [Column ("ID")] / / specify database corresponding table field name public long Id {get; set;} [Column ("EMPLOYEENO")] public int EmployeeNo {get; set;} [Column ("NAME")] public string Name {get; set } [Column ("BIRTHDAY")] public DateTime BirthDay {get; set;} [Column ("DEPARTMENT")] public string Department {get; set;} [Column ("ISVALID")] public bool IsValid {get; set;}}
After the above configuration, the query of the data can be executed normally.
Await using var context = new MyContext (options); var employeeCollection = await context.Employee.AsNoTracking (). ToListAsync (); / / var employeeCollection = await context.Employee.OrderBy (e = > e.EmployeeNo). AsNoTracking (). Skip (0). Take (5). ToListAsync (); / / paging foreach (var employee in employeeCollection) {Console.WriteLine ($"{employee.Name} | {employee.EmployeeNo} | {employee.Department}");} Console.ReadLine (); insert data
Inserting data is not much different from other databases, but because Oracle is born without self-incrementing Id, if the primary key of the table we designed needs to use self-increment key, we need to use Sequence instead. In this case, you need to specify the name of the Sequence for the primary key of the entity class.
ModelBuilder.Entity (entity = > {entity.ToTable ("EMPLOYEE"); entity.Property (e = > e.Id). IsRequired (); / /!! Specify the name of the sequence to be associated with! Entity.Property (e = > e.Id) .UseHiLo ("SEQ_EMPLOYEE_ID"); / / column mapping entity.Property (e = > e.Id) .HasColumnName ("ID"); entity.Property (e = > e.BirthDay) .HasColumnName ("BIRTHDAY"); entity.Property (e = > e.Department) .HasColumnName ("DEPARTMENT"); entity.Property (e = > e.EmployeeNo) .HasColumnName ("EMPLOYEENO"); entity.Property (e = > e.IsValid) .HasColumnName ("ISVALID") Entity.Property (e = > e.Name) .HasColumnName ("NAME");})
You can then perform the insert data.
Var employee = new Employee () {EmployeeNo = 6, Name = "Old Zhou", Department = "d6", BirthDay = DateTime.Now.AddYears (- 40), IsValid = true}; await using var context = new MyContext (options); var res = await context.Employee.AddAsync (employee); await context.SaveChangesAsync (); Console.WriteLine ("added successfully"); Console.ReadLine ()
The above is a simple summary of the use of EF Core to operate Oracle database when some special places, of course, I am still exploring practice, if there is new discovery will take the time to update up.
Thank you for reading, the above is the content of "how to configure EF Core to operate Oracle". After the study of this article, I believe you have a deeper understanding of how to configure EF Core to operate Oracle, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.