One-to-zero mapping with Entity framework

Asked

Viewed 39 times

0

Good afternoon, I have a document table that has relationship 1 to 0 with another table of Usuario to return the user name, however I am given the error:

Specified conversion is not valid.

My mapping:

public class DocumentoOffConfiguration : EntityTypeConfiguration<DocumentoOff>
{
    public DocumentoOffConfiguration()
    {
        ToTable("DOCUMENTO");

        this.Property(x => x.DataCriacao).HasColumnName("DATACRIACAO");
        this.Property(x => x.Enviadoar).HasColumnName("STATUS");
        this.Property(x => x.UsuarioId).HasColumnName("USUARIO_ID");

    }
}

In the View I try to show the following field:

<th>
@(Html.SortingColumn("Fiscal", nameof(Model.Usuario.Nome), 
    "Index", new { Model.Usuario.Nome }, 
        new AjaxOptions { UpdateTargetId = "div-gerenciador-auto-termos-index", 
        HttpMethod = "POST" 
    }))
</th>

public UsuarioOffConfiguration()
{
    this.ToTable("USUARIO");

    this.Property(x => x.UsuarioId)
        .HasColumnName("ID");

    HasMany(x => x.Documentos)
        .WithRequired(x => x.Usuario)
        .HasForeignKey(x => x.UsuarioId);
}

however it is returned me the above mentioned error, someone can give me a light on how to solve?

  • Where exactly does this mistake come from? There are ways to put the two relationships ?

  • the moment I click the query button to load the grid where the User Name must be shown on the grid, this name comes from the relationship of another table, which is what I said, need to know how I would make a relationship 1 to zero to return the user name

  • It would go like this: http://www.macoratti.net/Cursos/ef_curb1/ef_curb10.htm. take a look at this link has the relationship 1 to 1 which is the same from 1 to 0 -> One-to-One : Student and Alunoaddressee have a one-to-one relationshipto-one, i.e., Student owns zero or an Addressee;

  • What is your two relations is user and document?

  • I talked to a friend and he told me that in relationships 1 to zero no mapping is necessary, but the error continues to occur. In my domain of DOCUMENT I have a virtual public property User, which is to bring the name of the user to me, however the error occurs.

  • I can exemplify, just tell me is a user and or no document?

  • 1

    put the user table mapping to see how I am doing...

  • I took a look now at your question, and you posted the part that really matters in your last issue and the relationship 1 for 1 is very strange, in case your question is 1 for many, HasMany(x => x.Documentos) but, you can explain what is the expected relationship friend?

Show 3 more comments
No answers

Browser other questions tagged

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