3
I have two classes:
- User
- User
where Usuario
possesses its id
(PK) and UsuarioP
owns the id
(FK) of Usuario
.
I am working on ASP.NET MVC 4 using Fluent API.
Classes are mapped within the project to make connection to the bank. My project is divided into controllers, services, models and views.
I have a screen Usuario
using 1 field of UsuárioP
.
1) How can I instill it in the class without being in the way below? (That way it doesn’t work because of the relationship, otherwise it would work):
Class User.Cs
public string login { get { return this.UsuarioP.login; } set { login = value; } }
2) On the include screen, when adding 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 tried that way, but I couldn’t. In this case, the item.login
is only working because login is like [NotMapped]
in Usuario.cs
.
Summarizing: On the inclusion screen (Usuario
) I have a field that should bring from (UsuarioP
), the login.
When triggering the inclusion method, it should save the fields of the Usuario
and save login to UsuarioP
using IdUsuario
as key and also changing login status (TipoParticipante = 3
).
The mistakes I’ve already made
Invalid columm login (porque realmente `login` não existem em `Usuario`)
When debugging it includes only the fields of the Usuario
, and doesn’t even go through foreach
.
I don’t know how to make it work, can you help me? And if I’m not being clear, I put more details.
But for each added user I need to add a User with the status Typoparticipant = 3. base.context.Entry(usuarioP). State = Entitystate.Modified; I can do it ?
– O Aprendiz
You’re adding, not editing.
EntityState.Modified
is for editing only.– Leonel Sanches da Silva