5

I am using EF 6.1.3 Code First, but without migrations as the database already exists. I have an entity SRReports with the following property:

[Key, Required]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int FRID { get; set; }

Entity Framework ignores the DatabaseGeneratedOption.None and sends TSQL to the server assuming FRID is autogenerated. The entity is assigned a value for FRID but entity framework ignores it and assumes that is autogenerated. (this is confirmed by checking a TRACE of what is sent to the server). The exception message is:

Message=Cannot insert the value NULL into column 'FRID', table 'RevLogon.dbo.SCIREPORTS'; column does not allow nulls. INSERT fails. The statement has been terminated.

I tried using the fluent API instead of (and in addition to) the annotations.

 modelBuilder.Entity<SCIREPORTS>()
            .HasKey(e => e.FRID);
 modelBuilder.Entity<SCIREPORTS>()
            .Property(e =>  e.FRID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

But then I get the error when I query the table:

SCIREPORTS sr = mydb.SCIREPORTS.Where(s => s.FRID == FRID     ).FirstOrDefault();     

I get the following error:

System.MissingMethodException was caught HResult=-2146233069 Message=Method not found: 'System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration.HasDatabaseGeneratedOption(System.Nullable`1)'.*

I have uninstalled and reinstalled my EF package but haven't been able to solve this problem. How can I get EF to insert the record with the ID?

Here is the basic code that inserts the record:

public SRUserComp(string myemail, short mycounter, int myFRID, string myreptype, string mypassword)
    {   email = myemail;
        Counter = mycounter;
        reptype = myreptype;
        password = mypassword;
        FRID = myFRID;        }

public bool CreateSRRecord(DateTime repduedate,
        short repnumber)
{BSRModel mydb = new BSRModel(); 
sr = new SCIREPORTS();
sr.FRID = FRID;
sr.Counter = Counter;
sr.Final = true;
sr.RepDueDate = repduedate;
mydb.SCIREPORTS.Add(sr);
 mydb.SaveChanges();
return true; }   

After the saveChanges statement I get the following statements from the sql profiler on the server:

    INSERT [dbo].[SCIREPORTS]([Counter], [RepDueDate], [RepArrivalDate],
    [SciAppDate], [Adminappdate], [legacyDate], [RepNumber], [Final], 
    [RepReminderID], [Notes], [HaimSaw], [ResAuthApprovDate])

    VALUES (@0, @1, NULL, NULL, NULL, NULL, @2, @3, NULL, NULL, NULL, NULL)

    SELECT [FRID]
    FROM [dbo].[SCIREPORTS]
    WHERE @@ROWCOUNT > 0 AND [FRID] = scope_identity()      
Haim Katz
  • 455
  • 3
  • 15
  • How are you inserting the entity into the database? – DavidG Jun 02 '15 at 08:36
  • What are the data in the database after insert? Is the key (PK) assigned any values? – jonas Jun 02 '15 at 08:49
  • I added the additional code to answer your questions to the original post. – Haim Katz Jun 02 '15 at 10:03
  • "System.MissingMethodException" is very strange... It seems that you are using mismatching DLLs. Also check that you did not include the EF from the .Net Framework... Also the attribute not recognized could be a mismatching library... You could also try to use EF 6.1.2. – bubi Jun 02 '15 at 11:00
  • bubi. I suspect you are correct but I'm having trouble getting the correct EF. The EF file in the bin folder is 6.1.3 and that is the file referenced in the references of the project. I have uninstalled and installed EF twice. – Haim Katz Jun 02 '15 at 11:58
  • I finally gave up. After removing, erasing and reinstalling my nuget packages, I still couldn't get EF to accept the fact that my key was to be entered manually. I added a field ID to my entity and to the database (as an identity column) and now EF is able to insert an identity with the autogenerated ID column. – Haim Katz Jun 03 '15 at 11:03
  • I had the same issue, and I found this: http://stackoverflow.com/a/18917348/2066026 – Herr Kater Mar 09 '17 at 05:00

0 Answers0