IEntityChangeTracker =>的多个实例不能引用实体对象。使用存储库和DI

时间:2017-02-27 22:39:04

标签: c# entity-framework

在我的项目中,我使用了存储库并使用了依赖注入(Autofac Mvc)。

我的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();

}

我的IPropertyOptionLangRepository

   public interface IPropertyOptionLangRepository : IRepository<PropertyOptionLang>
{

}

我的PropertyOptionRepository(只需插入和保存方法)

 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;
        }
    }

和我的控制器(构造函数和插入方法)

    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();

    }

异常消息是:“e.Message”IEntityChangeTracker的多个实例无法引用实体对象。“

感谢您的帮助..

注意:我已搜索此异常消息,但我再次失败

编辑:对于Autofac配置,我创建了一个新类并更新了global.asax以进行启动。 `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));

    }
}`

0 个答案:

没有答案