简体   繁体   中英

Insufficient mapping in Entity Framework with fluent api (Database First) on 1:1 relationship

I have these classes and need to map then with relationship 1:1, but I not have yet a good knowledge for fluent API and I have received this error at runtime (debug):

"erro 3015: Problema nos fragmentos do mapeamento a partir das linhas 6, 22, 191:Restrição de chave estrangeira 'OperadorModel_OpCadastros1' da tabela OperadorModel (OpCadastros1_OperadorId) para a tabela OperadorModel (id):: Mapeamento insuficiente: a chave estrangeira deve ser mapeada para algum AssociationSet ou EntitySets participantes de uma associação de chave estrangeira no lado conceitual."

Something like:

"Error 3015: Problem in mapping fragments from lines 6, 22, 191: a foreign key constraint 'OperadorModel_OpCadastros1' table OperadorModel (OpCadastros1_OperadorId) for the table OperadorModel (id) :: Insufficient mapping: Foreign key must be mapped for some AssociationSet EntitySets or participating in a foreign key association in the conceptual. "

How to solve this problem?

The code is below. Thankfully, and sorry for my bad English.

public class OperadorModel
{
    public int OperadorId { get; set; }

    public string Login { get; set; }

    public bool TiraMenu { get; set; }

    public virtual OpCadastros1Model OpCadastros1 { get; set; }
}

public class OpCadastros1Model
{
    public int OperadorId { get; set; }

    public virtual OperadorModel Operador { get; set; }

    #region Apoio

    [Column("cad_apoio_ac")]
    [Display(Description = "Acessar", GroupName = "Cadastros", Name = "Laboratórios de Apoio")]
    public bool AcApoio { get; set; }

    [Column("cad_apoio_ad")]
    [Display(Description = "Adicionar", GroupName = "Cadastros", Name = "Laboratórios de Apoio")]
    public bool AdApoio { get; set; }

    [Column("cad_apoio_md")]
    [Display(Description = "Modificar", GroupName = "Cadastros", Name = "Laboratórios de Apoio")]
    public bool MdApoio { get; set; }

    [Column("cad_apoio_co")]
    [Display(Description = "Consultar", GroupName = "Cadastros", Name = "Laboratórios de Apoio")]
    public bool CoApoio { get; set; }

    [Column("cad_apoio_ex")]
    [Display(Description = "Excluir", GroupName = "Cadastros", Name = "Laboratórios de Apoio")]
    public bool ExApoio { get;  set; }

    #endregion
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    modelBuilder.Entity<OperadorModel>()
        .HasKey(op => op.OperadorId);

    modelBuilder.Entity<OpCadastros1Model>()
        .HasKey(cad => cad.OperadorId)
        .HasRequired(cad => cad.Operador)
        .WithRequiredPrincipal(cad => cad.OpCadastros1);

    modelBuilder.Entity<OperadorModel>().ToTable("operador", "public");
    modelBuilder.Entity<OpCadastros1Model>().ToTable("operador", "public");
}

I think your issue here is a need to specify the key to use explicitly as convention for how they are to be named is quite strict (it expects a column named ID - I think ), and something like this should fix the problem:

modelBuilder.Entity<OperadorModel>()
    .HasKey(op => op.OperadorId)
    .HasOptional(op => op.OpCadastros1)
    .Map(f => f.MapKey("OperadorID"));

After I wrote that I and looked for what was likely your ID, I realized the ID column is the same name in the Model for both objects...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM