Linq / Entity Framework不返回关联实体

时间:2015-07-02 19:35:35

标签: c# asp.net asp.net-mvc linq entity-framework

在我的_LoginPartialCompanyAdmin我有这个:

<a class="dropdown-toggle count-info" data-toggle="dropdown" href="#">
                @if (User.IsInRole("Company Admin"))
                {
                    @Html.Action("GetModules", "Module")
                }
</a>

我的模块控制器是这样的:

public ActionResult GetModules()
{
    return PartialView(Cache.Module.GetModulesForUser(ClaimsPrincipal.Current.Identities.First().Name));
}

缓存方法如下:

public static List<Models.ModulosPorUsuario> GetModulesForUser(string identityname)
{
    /// It needs to be cached for every user because every user can have different modules enabled.
    var cachekeyname = "ApplicationModulesPerUser-" + identityname;

    IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
    List<Models.ModulosPorUsuario> listOfModulesPerUser = new List<Models.ModulosPorUsuario>();
    listOfModulesPerUser = (List<Models.ModulosPorUsuario>)cache.Get(cachekeyname);

    if (listOfModulesPerUser == null)
    {
        listOfModulesPerUser = dbApp.ModulosPorUsuario.Where(p => p.Email == identityname).ToList();
        cache.Set(cachekeyname, listOfModulesPerUser, TimeSpan.FromMinutes(SettingsHelper.CacheModuleNames));
        return listOfModulesPerUser;
    }
    else
    {
        return listOfModulesPerUser;
    }
}

我的种子初始化方法是这样的:

#region Seed Modules
var module1 = new Module() { Id = 1, ModuleName = "Contabilidad", FontAwesomeClass = "fa-ambulance" };
var module2 = new Module() { Id = 2, ModuleName = "Recursos Humanos", FontAwesomeClass = "fa-heartbeat" };
var module3 = new Module() { Id = 3, ModuleName = "Inventario", FontAwesomeClass = "fa-anchor" };
var module4 = new Module() { Id = 4, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
var module5 = new Module() { Id = 5, ModuleName = "Produccion", FontAwesomeClass = "fa-binoculars" };
var module6 = new Module() { Id = 6, ModuleName = "Ventas", FontAwesomeClass = "fa-coffee" };
var module7 = new Module() { Id = 7, ModuleName = "Compras", FontAwesomeClass = "fa-calendar-o" };
var module8 = new Module() { Id = 8, ModuleName = "Cotizaciones", FontAwesomeClass = "fa-building" };

context.Modulos.Add(module1);
context.Modulos.Add(module2);
context.Modulos.Add(module3);
context.Modulos.Add(module4);
context.Modulos.Add(module5);
context.Modulos.Add(module6);
context.Modulos.Add(module7);
context.Modulos.Add(module8);
#endregion

#region Seed ModulosPor Usuario
context.ModulosPorUsuario.Add(new ModulosPorUsuario()
{
     Id = 1,
     Email = "companyadmin@xx.onmicrosoft.com",
     Modules = new List<Module>() { module1, module2 }
});

context.ModulosPorUsuario.Add(new ModulosPorUsuario()
{
    Id = 2,
    Email = "accountingadmin@xx.onmicrosoft.com",
    Modules = new List<Module>() { module3, module5 }
});

context.ModulosPorUsuario.Add(new ModulosPorUsuario()
{
    Id = 3,
    Email = "jayhamlin@xx.onmicrosoft.com",
    Modules = new List<Module>() { module4, module6 }
});

context.ModulosPorUsuario.Add(new ModulosPorUsuario()
{
    Id = 4,
    Email = "usuario1@xx.onmicrosoft.com",
    Modules = new List<Module>() { module7, module7 }
});

#endregion

然而,在调试Cache方法时,它没有为当前用户返回任何模块:

http://screencast.com/t/3UGd3NsR1

1 个答案:

答案 0 :(得分:0)

我发现错误,列表&lt;&gt;必须是虚拟ICollection:

public virtual ICollection<Module> Modules{ get; set; }