I can’t save a Dictionary

Asked

Viewed 77 times

4

I have a page that may have unauthorized access, open for reading or open for reading and writing. Even if the page is completely closed, the owner of the page can choose some users of the system that may have access to it. These exceptions can receive write or write and read permissions, for this, I used a Dictionary where the key will be the user who will have permission and the String will indicate what level of access it has on this page.

Problem: The Dictionary does not seem to be able to be saved in the bank, only in the Action that is being executed, thus it does not pass the Post of the page editing screen.

Page Model:

public class Fluxo
{
    [Key]
    public int FluxoID { get; set; }

    [Required(ErrorMessage = "Dê um nome a fluxo")]
    [Display(Name = "Título")]
    public String Nome { get; set; }

    [Required]
    [Display(Name = "Dono do fluxo")]
    public virtual Usuario Dono { get; set; }

    [Display(Name = "Topico")]
    public String TopicoPertencente { get; set; }

    [Required]
    [Display(Name = "Visibilidade")]
    public String Visibilidade { get; set; }

    public virtual IDictionary<Usuario, String> Permitidos { get; set; }

    public virtual IList<Informacao> Informacoes { get; set; }

    public object[] UsuarioID { get; internal set; }
}

Excerpt from Fluxocontroller/Edit:

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Edit([Bind(Include =   "FluxoID,Nome,TopicoPertencente,Visibilidade,desassociar")] ViewModels.Fluxo vwfluxo)
            {
              if (ModelState.IsValid)
              {
                Fluxo fluxo = db.Fluxo.Include(f => f.Dono).Where(fl => fl.FluxoID.Equals(vwfluxo.FluxoID)).FirstOrDefault();
                ...

                // Até essa parte, tudo vai bem, porém, a Action termina e o Dictionary se perde.
               fluxo.Permitidos = Selecionados; //Selecionados foi um Dictionary montado para salvar os usuários selecionados, essa lista irá preencher os Permitidos do objeto.

                db.Entry(fluxo).State = EntityState.Modified;
                db.SaveChanges();

    }

When leaving Action, flow. Allowed is null. I don’t know if something is missing to successfully save, I am a beginner in language and it is quite possible that something like this will happen, but my opinion is that there is some problem in trying to save a Dictionary in the bank, there is no attribute in db.Flow that seems to be related to this Dictionary. Sorry if something is complicated to understand, first post.

1 answer

3


Try not to be sad with what I say: the Entity Framework does not map dictionaries.

Create an associative class like this:

public class FluxoUsuario
{
    [Key]
    public int FluxoUsuarioID { get; set; }
    public int FluxoID { get; set; }
    public int UsuarioID { get; set; }

    public virtual Fluxo Fluxo { get; set; }
    public virtual Usuario Usuario { get; set; }
}

And Fluxo.cs:

public class Fluxo
{
    [Key]
    public int FluxoID { get; set; }

    [Required(ErrorMessage = "Dê um nome a fluxo")]
    [Display(Name = "Título")]
    public String Nome { get; set; }

    [Required]
    [Display(Name = "Dono do fluxo")]
    public virtual Usuario Dono { get; set; }

    [Display(Name = "Topico")]
    public String TopicoPertencente { get; set; }

    [Required]
    [Display(Name = "Visibilidade")]
    public String Visibilidade { get; set; }

    public virtual ICollection<FluxoUsuario> UsuariosPermitidos { get; set; }

    public virtual IList<Informacao> Informacoes { get; set; }

    public object[] UsuarioID { get; internal set; }
}

And Usuario.cs:

public class Usuario
{
    ...
    public virtual ICollection<FluxoUsuario> FluxosPermitidos { get; set; }
}
  • Okay, I get the point, but I’m still having a question, I need to differentiate the permissions, with this I can see that I will know which user has permission in which stream, but I still don’t know how to differentiate the permissions ("Read and write", "Read only"). I must add an attribute to the associative class?

  • Yes. My suggestion, by the way, is a Enum.

  • 1

    All right! I just added an Enum typePermissao { Read, Lerwrite}.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.