0

What i have initially on my App is a list of expensives that is created based on the Scarfolding System but that list is the same for each User, and what i want is that each user can create his own list of expensives and see his own data.

So in the expensive class i did this:

public class Despesa
{
    public int TipoDespesaId { get; set; }

    public int DespesaId { get; set; }

    public string UserId { get; set; }

    [Display(Name = "Descrição da Despesa")]
    [Required]
    public string DespesaDescricao { get; set; }

    [Display(Name = "Valor")]
    [Required]
    public decimal DespesaValor { get; set; }

    public int TipoPagamentoId { get; set; }

    [Display(Name = "Data")]
    [DataType(DataType.Date)]
    [CustomValidation(typeof(Validator), "ValidateEndTimeRange")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
    [Required]
    public DateTime Data { get; set; }

    public TipoDespesa TipoDespesa { get; set; }

    public TipoPagamento TipoPagamento { get; set; }

    [Display(Name = "Comentário")]
    public string Comentario { get; set; }

}

i just passed the UserId to the model and then in the Index controller of my Expensive View i did a linq query to compare the currentUserID to the Id of the expensive User here is my code:

public ActionResult Index()
    {
        String userId = User.Identity.GetUserId();
        var despesas = from r in db.Despesas.Include(d => d.TipoDespesa).Include(d => d.TipoPagamento).Include(d => d.UserId)
                       where r.UserId.Equals(userId)
                       select r;

        return View(despesas.ToList());
    }

what i need to know is what i am doing wrong cause i get a invalidOperationException

caxinaswin
  • 71
  • 3
  • 9

1 Answers1

0

Only navigation properties can be used with .Include() it seems.

You are trying to include a primitive property (UserId), and it then throws the error when converting to a list because it has no navigation property.

var despesas = from r in db.Despesas.Include(d => d.TipoDespesa).Include(d => d.TipoPagamento).Where(x => x.UserId == userId) select r;
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
  • it still doestn work im having this errors: MSDiary.DAL.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. MSDiary.DAL.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined Is there other way to do this without including the foreign key String in my Despesas Model?? Sorry for newbie questions – caxinaswin Mar 17 '16 at 16:15
  • @caxinaswin That's a different problem, you should post another thread if you can't fix. See http://stackoverflow.com/questions/19994590/asp-net-identity-validation-error and http://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit Even if you don't do it this way, you will still need to fix this problem at some stage. I don't see any other way of doing it without using a unique key. – Martin Dawson Mar 17 '16 at 16:30