0

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

            }
Red
  • 2,728
  • 1
  • 20
  • 22
Danny
  • 301
  • 1
  • 4
  • 21
  • Are you using your DB Entities in ViewModels/Views? I would strongly advise not to, and load entity from DB manually instead of using change tracker for this. This code becomes hard to control and maintain very fast. – Red Feb 12 '16 at 08:03
  • I'm open to suggestion , what do you suggest ? – Danny Feb 12 '16 at 08:46
  • The first thing to do here, I believe, is get rid of your DB Model in ViewModel, and use ViewModels fully on their own instead - even for your `model` that you edit here. After that, in your method you should load your current `model` from the database by Id, taken from ViewModel that you use to edit it and update properties in DB Model from the properties of ViewModel manually – Red Feb 12 '16 at 09:26
  • I'm not sure I understand – Danny Feb 12 '16 at 09:54
  • Could you include whole method body with name, parameters + definition of Type of your model in your post, so that I could show it on your code? – Red Feb 12 '16 at 13:25
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:30

0 Answers0