4

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?

5
  • Did you have EF create the database or you are using CodeFirst with a database that already existed?
    – Pawel
    Apr 12, 2012 at 3:45
  • I'm using CodeFirst with an existing database, sice Oracle doesn't support database creation yet. But the table doesn't have any weird. The ID field is the primary key with NUMBER(9) as type.
    – fcaldera
    Apr 12, 2012 at 17:05
  • 3
    I don't know Oracle but this table docs.oracle.com/cd/E11882_01/win.112/e18754/featLINQ.htm shows that NUMBER type can be mapped to Edm.Decimal type (I assume Number(9) is something different than Number(9,0) and therefore falls to "other cases"). You can confirm this either by dumping your edmx (EdmxWriter.WriteEdmx) and checking the EDM type of the CSpace key property of the Something entity or changing the type in your class to Decimal to see if it will work.
    – Pawel
    Apr 12, 2012 at 20:54
  • 1
    Hey Pawel, althuoght I'm not using an edmx file since CodeFirst doesn't need that, changing the type to decimal into my POCO class make it works. Thanks!
    – fcaldera
    Apr 14, 2012 at 21:55
  • 3
    CodeFirst internally creates Edmx. CodeFirst is built on ObjectContext and ObjectContext cannot work without Edmx (or to be more correct CSDL, SSL and MSL artifacts). So, what CodeFirst does is building EDMX files based on the object model (i.e. classes). If you want to see Edmx for your CodeFirst model you can use EdmxWriter.WriteEdmx() method.
    – Pawel
    Apr 15, 2012 at 3:42

1 Answer 1

4

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; }
    }
3
  • 1
    Correct me if I'm wrong, but you should also be able to add the following attribute to the Id property in the class definition: [Column("ID", TypeName="INT")] Oct 26, 2012 at 14:44
  • I didn't try it, probably it will work too. But anyway I prefer to use the Fluent API.
    – msi
    Oct 26, 2012 at 20:00
  • I guess that's why the Fluent API is there, just pointing out the alternative Oct 26, 2012 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.