Questions about Fluent API relationship for EF 5

Asked

Viewed 109 times

5

I have two classes:

  • User > Contains your PK (ID).
  • User > Contains the user ID(FK).

Class User.Cs

public Usuario()
{
    this.UsuariosP = new List<UsuarioP>();
}

public int Id { get; set; }

(...)
public virtual ICollection<UsuarioP> UsuariosP { get; set; }

Class Usuariop.Cs

public partial class UsuarioP
{
    public int Id { get; set; }
    public Nullable<int> IdUsuario{ get; set; }

    public string Login { get; set; }
    (...)

    public virtual Usuario Usuario{ get; set; }
}

The classes were mapped like this.

I’d like to call the Login that’s inside UsuárioP in Usuario. I can’t because of their relationship, otherwise I could.

Attempts - I have already created a virtual User attribute within User. (Unsuccessful) - I have tried to urge you in this way:

public string login {get { return this.UsuarioP.Login} } (Sem sucesso)
  • Putting Login in Usuario.cs as [NotMapped] (Unsuccessful)

Important: I want to do the get and set of this attribute. My project is working with controllers, services, models and views.

How to call the Login inside UsuarioP, so I can use it on the User screens?

Note: Working with MVC 4
EPH 5
Fluent API

2 answers

5


There is nothing wrong with your code. The mapping is perfect. There is, yes, something wrong in the way you are using.

Usuario has N UsuarioP (cardinality 1 to N). That is, you will also have N Logins, being one for UsuarioP.

You want to implement the get and set of Login, but they are already implemented automatically:

public string Login { get; set; }

The Entity Framework takes care of uploading the information to you at the time it is used. Possibly you are using Login incorrectly. I’ll give you some examples of how you might be recovering this Login.

1. Iterating on the collection UsuariosP

Your View can do the following:

@foreach (var usuarioP in Model.UsuariosP)
{
    <div>@usuarioP.Login</div>
}

2. Locating a specific record

Also in the View:

<div>@Model.UsuariosP.First().Login</div>
@{
    var teste = @Model.UsuariosP.FirstOrDefault(u => u.Id == 2);
}
@if (teste != null) {
    <div>@teste.Login</div>
}
  • I should use a template inside the login attribute. This way you made the 1) .. I could put the template?

  • Yes, but that’s another question. You can do and paste the link here and I’ll answer it for you.

  • I asked a question yesterday about that.. assuming that the initial idea was how to get this attribute, I did not get help: pq actually what I really need is this (I am totally lost) :http://answall.com/questions/89318/met%C3%B3do-include-mvc-working-with-inser%C3%A7%C3%A3o-of-data-in-two-tables

  • I wanted to use the get of this attribute and make sure that at the time of inserting on the include screen it gets the value for the other table...

0

Probably your context is configured with the Lazyloading (Lazy loading) enabled. Lazyloading is used for loading related entities only when we call the mapped property. In the case of MVC this does not work, because the access to the information is carried out in the client’s layer, therefore this loading must be disabled.

According to the ASP.NET documentation in addition to this problem the lazy loading may cause a slow loading of the information, since the number of requests may be higher than normal so that the serialization is performed correctly.

Browser other questions tagged

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