Metadata Include - MVC - working with insertion of data into two tables

3

I have two classes:

  • User
  • UsuarioP

where Usuario has its id (PK) and UsuarioP has id (FK) of Usuario .

I'm working on ASP.NET MVC 4 using Fluent API.

The classes are mapped inside the project to connect to the bank. My project is divided into controllers , services , and views .

I have a Usuario screen that uses 1 UsuárioP field.

1) How can I instantiate it in the class without being as below? (so it does not work because of the relationship, otherwise it would work):

User.cs class

public string login { get { return this.UsuarioP.login; } set { login = value; } }

2) In the include screen, when adding items from Usuario , I should also change the screen status, this status is within UsuarioP .

Controller

        public ViewResultBase Incluir(Usuario model)
        {
            if (this.ModelState.IsValid)
            {
                try
                {

                    this.Service.SalvaUsuarioP(model);
                    return this.SuccessView(true);
                }
                catch (ValidationException exception)
                {
                    base.AddValidationErrors(exception);
                    return base.PartialView(model);
                }
            }
            else
            {
                return base.PartialView(model);
            }
        }

Service

        public void SalvaUsuarioP(Usuario item)
        {
            //Salva os campos de Usuario(está funcionando perfeitamente)
            base.context.Usuario.Add(item);

            //Tentativa para salvar o login e o seu estado em UsuarioP
            foreach (var usuarioP in base.context.UsuariosP.Where(x => x.IdUsuario == item.Id))
            {
                item.login = usuarioP.Login;
                usuarioP.TipoParticipante = 3;
                base.context.UsuariosP.Add(usuarioP);
            }

        }

I've tried it that way, but I could not. In this case, item.login is only working because login is as [NotMapped] in Usuario.cs .

In short: In the inclusion screen ( Usuario ) I have a field that should bring ( UsuarioP ), login.

At the time of triggering the include method, it should save the fields of Usuario and save login to UsuarioP using IdUsuario as key and also changing login status ( TipoParticipante = 3 ).

The errors I've already gotten

Invalid columm login (porque realmente 'login' não existem em 'Usuario')

At the time of debugging it includes only the fields of Usuario , and does not even go through foreach .

I do not know how to make it work, can you help me? And if I have not been clear, I'll put more details.

    
asked by anonymous 27.09.2015 / 22:50

1 answer

3

First of all, it is important to know that Usuario has cardinality 1 for N with UsuarioP , otherwise it is impossible to answer this question.

  

1) How can I instantiate it in the class without being as below?

public string login {get { return this.UsuarioP.login} set { login = value} }

In fact, this is not going to work because the cardinality is 1 for N, meaning you would have to define which of the% N of% s you would be changing.

  

2) In the inclusion screen when adding the User items, I should also change the status of the screen, this status is within UserP.

For the same reason, this part of the service code is wrong, and for more reasons:

        //Tentativa para salvar o login e o seu estado em UsuarioP
        foreach (var usuarioP in base.context.UsuariosP.Where(x => x.IdUsuario == item.Id))
        {
            item.login = usuarioP.Login;
            usuarioP.TipoParticipante = 3;
            base.context.UsuariosP.Add(usuarioP);
        }

It makes no sense to select multiple Login , then assign UsuarioP to itself. I think there's a lot of confusion there.

If you want to change the status of Login , the code below resolves:

        foreach (var usuarioP in item)
        {
            usuarioP.TipoParticipante = 3;
            base.context.Entry(usuarioP).State = EntityState.Modified;
        }

Or, if the registry is new:

        foreach (var usuarioP in item)
        {
            usuarioP.TipoParticipante = 3;
            base.context.UsuariosP.Add(usuarioP);
        }

Or better yet, you can set the default type in the constructor:

public UsuarioP() {
    TipoParticipante = 3;
}

Do not use the related class to do assignments until concepts about working with the Entity Framework are properly fixed.

    
28.09.2015 / 16:43