3

I have read all post related to this and i've tried them all but not results.

Im using fluent api to map my models to the database. But when im query i get this Error:

Method not found:

 'System.Data.Entity.ModelConfiguration.Configuration.DecimalPropertyConfiguration

System.Data.Entity.ModelConfiguration.Configuration.DecimalPropertyConfiguration.HasDatabaseGeneratedOption(System.Nullable`1<System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption>)'.

My model look like this:

ToTable("table_name");
    HasKey(x => x.CounterId)//this property IS NOT NULLABLE
        .Property(x => x.CounterId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
        .HasColumnName("dbCounter_Id")
        .HasPrecision(10, 0)
        .IsRequired();

    Property(x => x.HouseId)
        .HasColumnName("dbHouseId");

    Property(x => x.ApplicationId)
        .HasColumnName("dbApplication_id");

For some reason .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) is giving me this error so when i quit it:

HasKey(x => x.CounterId)
    .Property(x => x.PageId)
    .HasColumnName("dbCounter_Id")
    .HasPrecision(10, 0)
    .IsRequired();

I get not error but as im lucky as hell, when im trying to add a record to that table i get insert identity exception. I cant neither added nor quit it the HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) method.

Important: The key CounterId is not type INTEGER, is DECIMAL(10,0) instead. Could that be the problem here? I cannot change the datatype of the column since there are a lot of app in production that will be affect in the worst way.

Hope i can get any help.

Community
  • 1
  • 1
Einer
  • 505
  • 1
  • 6
  • 15
  • Just tried and it works for me. When exactly do you have this error you've mentioned? – mr100 May 09 '14 at 18:36
  • 1
    With HasDatabaseGeneratedOption method on, whenever i try to query i get not method found and without the HasDatabaseGeneratedOption method whenever i try to insert i get the INSERT IDENTITY EXCEPTION – Einer May 09 '14 at 18:38
  • Your code works perfectly for me. – mr100 May 09 '14 at 18:39

4 Answers4

4

You should make it in this way

ToTable("table_name");
    HasKey(x => x.CounterId); // you should split HasKey from Property
    Property(x => x.CounterId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
        .HasColumnName("dbCounter_Id")
        .HasPrecision(10, 0)
        .IsRequired();

    Property(x => x.HouseId)
        .HasColumnName("dbHouseId");

    Property(x => x.ApplicationId)
        .HasColumnName("dbApplication_id");
Monah
  • 6,714
  • 6
  • 22
  • 52
2

You should try to remove the nuget package from all projects. Then add it again.

I don't know why but it just worked for me, even if the package was the same.

emp
  • 4,926
  • 2
  • 38
  • 50
0

One of the reasons for this error is because of different .NET versions of your projects. For e.g. if your EF DbContext class is created in a project of .NET v4.5, and if the Web Application you are using it in is of .NET v4.0, then you might get this error at run-time.

Ideally, it should error at compile time. But, some times Visual Studio compiles fine, especially for older "Website" type projects. And, when the application actually runs, that is when the error occurs.

So, to fix this, make sure that .NET framework version of your client application (that uses the EF DbContext class) is >= .NET version of project containing the DbContext.

Krishnan
  • 1,464
  • 15
  • 21
0

I had a similar issue with EF6 and ASP.Net 3.1. This answer helped nudge me in the right direction, but I solved it slightly differently. All I needed to do was add the following in my model class that the table was based on:

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Round { get; set; }
tp803
  • 117
  • 12