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

0

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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 That's a different problem, you should post another thread if you can't fix. See stackoverflow.com/questions/19994590/… and stackoverflow.com/questions/28531201/… 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.