0

In my project ,I used repository and used dependency injection (Autofac Mvc).

My IReposiroty =

 public interface IRepository<T> where T:class
{
    IEnumerable<T> GetAll();
    T GetById(int id);
    T Get(Expression<Func<T,bool>> expression);
    IQueryable<T> GetMany(Expression<Func<T, bool>> expression);
    bool Insert(T obj);
    bool Update(T obj);
    bool Delete(int id);
    int Count();
    bool Save();

}

My IPropertyOptionLangRepository

   public interface IPropertyOptionLangRepository : IRepository<PropertyOptionLang>
{

}

My PropertyOptionRepository (Just Insert and Save Methods)

 public bool Insert(PropertyOptionLang obj)
    {
        try
        {
            _database.PropertyOptionLang.Add(obj);
            var num=_database.SaveChanges();

            return true;
        }
        catch(Exception e)
        {
            Console.WriteLine(e);
            return false;
        }
    }


    public bool Save()
    {
        try
        {
            _database.SaveChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

And My Controller ( constructor and Insert Method)

    private readonly IPropertyOptionRepository _propertyOptionRepository ;
    private readonly IPropertyOptionLangRepository      _propertyOptionLangRepository ;
    private readonly ILanguageRepository _languageRepository;



    public FieldController(IPropertyOptionRepository propertyOptionRepository,
                           IPropertyOptionLangRepository propertyOptionLangRepository,
                           ILanguageRepository languageRepository)
    {
        _languageRepository = languageRepository;
        _propertyOptionRepository = propertyOptionRepository;
        _propertyOptionLangRepository = propertyOptionLangRepository;
    }
public ActionResult Add()
  {
         PropertyOptionLang test3 = new PropertyOptionLang();
         var option = _propertyOptionRepository.GetById(2);
         var lang2 = _languageRepository.GetById(2);
         test3.Language = lang2;
         test3.PropertyOption = option;
         test3.Name = "hasan";
         test3.Prefix = "test2";
         test3.Value = "aaa";
         _propertyOptionLangRepository.Insert(test3);
         _propertyOptionLangRepository.Save();

    }

exception message is : " e.Message "An entity object cannot be referenced by multiple instances of IEntityChangeTracker."

thanks for help..

Note: I search already for this exception message but I failed again

Edit: For Autofac configurate I created a new class and update global.asax for startup. `public static class Bootstrapper {

    public static void RunConfig()
    {
        BuildAutofac();
    }

    private static void BuildAutofac()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(MvcApplication).Assembly);
        builder.RegisterType<LanguageRepository>().As<ILanguageRepository>();
        builder.RegisterType<PropertyOptionLangRepository>().As<IPropertyOptionLangRepository>();
        builder.RegisterType<PropertyOptionRepository>().As<IPropertyOptionRepository>();




        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    }
}`
  • How have you configured your Autofaq container? This error is in regards to having multiple DbContext's being used within a single life-cycle, so I'm willing to bet your repositories are using different DbContexts, which would cause this issue. – James Haug Feb 27 '17 at 22:56
  • Possible duplicate of [entity object cannot be referenced by multiple instances of IEntityChangeTracker. while adding related objects to entity in Entity Framework 4.1](http://stackoverflow.com/questions/10191734/entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetracker) – Erik Philips Feb 27 '17 at 22:57
  • @JamesHaug I added now in post how I configured my autofac. – Umut Bayğut Feb 27 '17 at 23:08
  • And I use " private readonly DatabaseContext _database = new DatabaseContext();" in all repository – Umut Bayğut Feb 27 '17 at 23:11
  • @ErikPhilips thanks for message but I saw this post but I failed. I didnt understand for my code – Umut Bayğut Feb 27 '17 at 23:12
  • @UmutBayğut You need to remove `private readonly DatabaseContext _database = new DatabaseContext();`. Instead, add this line to your `BuildAutofac` above: `builder.RegisterType()`. Then, each of your repositories should set the `DatabaseContext` field/property via a constructor that accepts `DatabaseContext` – James Haug Feb 27 '17 at 23:23
  • @JamesHaug Okey I can add builder.RegisterType() in BuildAutofac , For DatabaseContext private DatabaseContext _database; public PropertyOptionLangRepository(DatabaseContext databaseContext) { _database = databaseContext; } is it correct ? – Umut Bayğut Feb 27 '17 at 23:29
  • @JamesHaug I add 'builder.RegisterType()' code in BuilAutofac. After that Update All repository. => Deleted 'private readonly DatabaseContext _database = new DatabaseContext();' and add on all repository ctor with paremeter and add 'private readonly DatabaseContext _database;' . so after update repository was be example ' private readonly DatabaseContext _database; public PropertyOptionLangRepository(DatabaseContext database) { _database = database; }' but stil have a error and error message is same. – Umut Bayğut Feb 27 '17 at 23:53
  • @UmutBayğut You will also need to adjust the registration to reflect `InstancePerLifetimeScope` as found here: http://docs.autofac.org/en/latest/lifetime/instance-scope.html#instance-per-lifetime-scope – James Haug Feb 28 '17 at 01:50
  • @JamesHaug thanks its run now :) – Umut Bayğut Feb 28 '17 at 11:39

0 Answers0