EF ignores DatabaseGeneratedOption.None
I am using EF 6.1.3 Code First but no migration as the database already exists. I have an object SRReports
with the following property:
[Key, Required]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int FRID { get; set; }
Entity Framework ignores DatabaseGeneratedOption.None
and sends TSQL to server assuming FRID is autogenerated. The object is assigned a value for FRID, but the frame structure ignores it and assumes that it is auto-generated. (this is confirmed by a TRACE check of what is sent to the server). Exception message:
Message = Unable to insert NULL value in column "FRID", table "RevLogon.dbo.SCIREPORTS"; the column does not allow zeros. INSERT fails. The application has been completed.
I tried to use fluent API instead of (and in addition to) annotations.
modelBuilder.Entity<SCIREPORTS>()
.HasKey(e => e.FRID);
modelBuilder.Entity<SCIREPORTS>()
.Property(e => e.FRID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
But then I get an error when I query the table:
SCIREPORTS sr = mydb.SCIREPORTS.Where(s => s.FRID == FRID ).FirstOrDefault();
I am getting the following error:
Fixed System.MissingMethodException HResult = -2146233069 Message = Method not found: 'System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration.HasDatabaseGeneratedOption (System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration. *
I uninstalled and reinstalled my EF package but was unable to resolve the issue. How can I force EF to insert a record with an ID?
Here's 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 instructions 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()
source to share
No one has answered this question yet
See similar questions:
or similar: