I can not save a Dictionary

4

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

Problem: The dictionary does not seem to be able to be saved in the database, just in the Action that is being executed, so it is just the Post from 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; }
}

FluxController / Edit Excerpt:

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

    }

On exit Action, flow.Permitted is null. I do not know if something is missing to save successfully, I am a beginner in the language and it is quite possible that something like this happens, but my opinion is that it has some problem in trying to save a dictionary in the database, there is no attribute in db. seems to be related to this dictionary. Sorry if something is complicated to understand, first post.

    
asked by anonymous 23.12.2015 / 20:04

1 answer

3

Try not to be sad about what I'm going to 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; }
}
    
23.12.2015 / 20:22