S
S
SvetlyiAkaPro2014-02-18 21:37:53
Database
SvetlyiAkaPro, 2014-02-18 21:37:53

How to fix the error with saving two entities in the database?

Available:
public class DataContext : DbContext

{
.......
public DbSet<Операция> Операции { get; set; }
public DbSet<Документ> Документы { get; set; }
.......
}

Accordingly, there are models Document, Operation
When I add a document to the database in the controller, then everything is fine
.......
db.Документы.Add(документ);
db.SaveChanges();

But when I try to write multiple entities:
db.Документы.Add(документ);
Операция log = new Операция("Добавление документа", "Название документа - "+документ.Название);
db.Операции.Add(log); //Тут ошибка
db.SaveChanges();

then an error occurs: "Several instances of the IEntityChangeTracker interface cannot refer to an entity object"
PS Please, don't say anything about Russian variable names.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SvetlyiAkaPro, 2014-02-19
@SvetlyiAkaPro

which one?

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FinCooperation.Models
{
    public class Документ
    {
        [Key]
        public int ДокументID { get; set; }
        [Required]
        public string Название { get; set; }
        public System.DateTime Добавлен { get; set; }
        public System.DateTime Обновлен { get; set; }
        public byte[] ФайлДокумента { get; set; }
        [HiddenInput(DisplayValue = false)]
        public string MIMEтип { get; set; }

        public virtual Кооператив Кооператив { set; get; }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace FinCooperation.Models
{
    public class Операция
    {
        [Key]
        public int ОперацияID { get; set; }
        [Required]
        public string Наименование { get; set; }
        [Required]
        public System.DateTime Дата { get; set; }
        public string Детали { get; set; }
        public Операция(string name, string details)
        {
            this.Наименование = name;
            this.Дата = DateTime.Now;
            this.Детали = details;
            this.Пользователь = (Пользователь)HttpContext.Current.Session["User"];
        }
        public Операция(string name)
        {
            this.Наименование = name;
            this.Дата = DateTime.Now;
            this.Пользователь = (Пользователь)HttpContext.Current.Session["User"];
        }
        //Пользователь
        [Required]
        public virtual Пользователь Пользователь { get; set; }
    }
}

C
coolfusion, 2014-02-19
@coolfusion

Give more information.
In the meantime here is stackoverflow.com/questions/2639384/error-an-entit...

E
eRKa, 2016-07-30
@kttotto

How was the solution found? And then I ran into a similar problem, I did not find an adequate solution (

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question