EF 5下Oracle NUMBER(5)字段类型适配问题求助
Got it, this is a super common pain point with EF's default type mapping for Oracle NUMBER columns. The good news is you don't need to touch your database at all—we can fix this entirely on the entity model side.
The root issue here is EF5 automatically maps Oracle's NUMBER(5) to short (Int16), which tops out at 32767, but your 95xxx values fit perfectly in an int (Int32). Here are the three most straightforward ways to override this default mapping:
1. Use Data Annotations (Code-First)
Add the [Column] attribute to your entity property to explicitly tie it to an int while keeping alignment with the database's NUMBER(5) type:
using System.ComponentModel.DataAnnotations.Schema; public class YourTargetEntity { // Replace "YourIdField" with your actual property name [Column(TypeName = "NUMBER(5)")] public int YourIdField { get; set; } }
This tells EF to treat the property as an int in your code, while still matching the existing database column type.
2. Use Fluent API (Code-First/Model-First)
If you prefer centralized mapping configuration, use the Fluent API in your DbContext's OnModelCreating method:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<YourTargetEntity>() .Property(e => e.YourIdField) .HasColumnType("NUMBER(5)") // Matches the database column definition .HasPrecision(5, 0); // 5 total digits, 0 decimal places (matches your NUMBER(5)) // EF will default to int for this property since we're not forcing a smaller type }
3. Modify EDMX Model (Database-First)
If you're working with an EDMX file generated from the database:
- Open your EDMX in the designer
- Locate the entity property for your ID field
- Open the Properties window (press F4 if hidden):
- Change the Type from
Int16toInt32 - Double-check that Column Type still reads
NUMBER(5)
- Change the Type from
All these approaches let EF load and save your 95xxx values without overflow errors, while leaving your existing database schema and dependent applications untouched.
内容的提问来源于stack exchange,提问作者ilhank

