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.