Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

entity framework - Oracle ODP.Net and EF CodeFirst - edm.decimal error

I have the following simple entity:

public class Something{
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public int ID { get; set; }
    public string NAME { get; set; }
    public int STATUS { get; set; }
}

As you can see, I do not want the ID is generated from the database but I'm going to enter manually. This my DbContext class:

public class MyCEContext : DbContext {
    ...
    public DbSet<Something> Somethings { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        string dbsch = "myce";
        modelBuilder.Entity<Something>().ToTable("SOMETHING", dbsch);
    }
}

There is nothing special here. But this code fails:

            using (MyCEContext ctx = new MyCEContext()) {
                Something t = new Something();
                t.ID= 1;
                t.NAME = "TEST";
                t.STATUS = 100;

                ctx.Somethings.Add(t);
                ctx.SaveChanges();
            }

This is the error:

The specified value is not an instance of type 'Edm.Decimal'

In general, allways EF try to send a value to an int primary key field, I get the edm.decimal error.

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I commented on previous answer, I've found better solution, it is strange, but it works

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<TestEntity>().ToTable("TESTENTITY", "SCHEMENAME");
            modelBuilder.Entity<TestEntity>().Property(p => p.Id).HasColumnName("ID").HasColumnType("INT");
            modelBuilder.Entity<TestEntity>().Property(p => p.TestDateTime).HasColumnName("TESTDATETIME");
            modelBuilder.Entity<TestEntity>().Property(p => p.TestFloat).HasColumnName("TESTFLOAT");
            modelBuilder.Entity<TestEntity>().Property(p => p.TestInt).HasColumnName("TESTINT");
            modelBuilder.Entity<TestEntity>().Property(p => p.TestString).HasColumnName("TESTSTRING");
        }

and TestEntity looks like this

public class TestEntity
    {
        public int Id{ get; set; }

        public string TestString { get; set; }

        public int TestInt { get; set; }

        public float TestFloat { get; set; }

        public DateTime TestDateTime { get; set; }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...