while using this variable i'm getting this error:
Attaching an entity of type ' ' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
That force me to change from :
db.Entry(model.sub).State = EntityState.Modified; to : EntityState.Added (create new Db entry and I do not want it )
the code :
{
var loggedInUserId = User.Identity.GetUserId();
var member = await db.member.SingleOrDefaultAsync(m => m.ApplicationUserId == loggedInUserId);
//check if the subject name already exists.
var match = await db.Subject.Select(s => s.SubjectName).FirstOrDefaultAsync(subjectName => subjectName.Equals(model.sub.SubjectName));
model.rev.Created = DateTime.Now;
if (!ModelState.IsValid)
{
return View(model);
}
bool comper = string.Equals(model.sub.SubjectName, match, StringComparison.InvariantCultureIgnoreCase);
if (comper == true)
{
var getSubject = (from c in db.Subject.Where(aa => aa.SubjectName.ToString().ToLower().Equals(match)) select c);
var GetSubjectId = await (from a in getSubject select a.SubjectId).SingleAsync();
var R_getvelue = await getSubject.SingleAsync();
if (model.rev.GBU == "Good")
{
//Error
var Good = R_getvelue.Good;
var Bad = R_getvelue.Bad;
var Ugly = R_getvelue.Ugly;
//OK
//var Good = await (from a in getSubject select a.Good).SingleAsync();
//var Bad = await (from b in getSubject select b.Bad).SingleAsync();
//var Ugly = await (from u in getSubject select u.Ugly).SingleAsync();
model.sub.Bad = Bad;
model.sub.Ugly = Ugly;
int iGoodRating = Convert.ToInt32(Good);
iGoodRating++;
model.sub.Good = iGoodRating;
model.sub.SubjectId = GetSubjectId;
model.sub.Total = Convert.ToInt32(model.sub.Ugly + model.sub.Bad + model.sub.Good);
db.Entry(model.sub).State = EntityState.Added
}