Mapping equivalence between Fluentnhibernate and Entityframeworkcore?

Asked

Viewed 19 times

0

I am on a project where it is necessary to exchange the ORM from Nhibernate to Entityframeworkcore. With this, all the mappings will have to be rewritten to the Entity standard and I’m having some difficulties in redoing the classes. Do I get anything on the Web that addresses that transcript? Ex: the class below is written in Fluentnhibernate:

public AgendaGrupoMap()
{
    Table("AGENDA_GRUPO");

    Id(x => x.Id).Column("id_agenda_grupo");

    Map(x => x.IdMedico, "id_medico");
    Map(x => x.IdUnidade, "id_unidade");
    Map(x => x.VigenciaInicio, "dt_vigencia_inicio");
    Map(x => x.VigenciaTermino, "dt_vigencia_termino");
    Map(x => x.Ativo, "fl_ativo");

    HasMany<Agenda>(x => x.Agendas)
        .KeyColumn("id_agenda_grupo")
        .Cascade.None()
        .Not.KeyUpdate();

    References(x => x.Medico)
    .Class<Medico>()
    .Columns("id_medico")
    .Not.Update()
    .Not.Insert();
}

and the rewriting for Entity is as follows:

public void Configure(EntityTypeBuilder<AgendaGrupo> builder)
{
    builder.HasKey(x => x.Id);

    builder.Property(x => x.Id)
        .HasColumnName("id_agenda_grupo");

    builder.Property(x => x.IdMedico)
        .HasColumnName("id_medico");

    builder.Property(x => x.IdUnidade)
        .HasColumnName("id_unidade");

    builder.Property(x => x.VigenciaInicio)
        .HasColumnName("dt_vigencia_inicio");

    builder.Property(x => x.VigenciaTermino)
        .HasColumnName("dt_vigencia_termino");

    builder.Property(x => x.Ativo)
        .HasColumnName("fl_ativo");

    builder.HasMany(x => x.Agendas).WithOne(x => x.AgendaGrupo);

    builder.ToTable("AGENDA_GRUPO");

In this rewrite I don’t know how to rewrite this excerpt (References) to the Entity:

References(x => x.Medico)
    .Class<Medico>()
    .Columns("id_medico")
    .Not.Update()
    .Not.Insert();

Can you suggest something to me? Thank you...

  • And then you clarified the answer Something helped you?

1 answer

0

Of Nhibernate Map:

References(x => x.Medico)
    .Class<Medico>()
    .Columns("id_medico")
    .Not.Update()
    .Not.Insert();

for Entity Framework Core

builder.HasOne(x => x.Medico)
    .WithMany(c => c.AgendaGrupo)
    .HasForeignKey("id_medico");

Of course I transferred a chunk of code from one to the other, but on the Microsoft website has the complete documentation mainly in this part of relationships

An example of 1 for many:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFModeling.FluentAPI.Relationships.NoForeignKey
{
    #region Model
    class MyContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Post>()
                .HasOne(p => p.Blog)
                .WithMany(b => b.Posts);
        }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public Blog Blog { get; set; }
    }
    #endregion
}

Reference: https://docs.microsoft.com/pt-br/ef/core/modeling/relationships#Fluent-api

I’m going to have a prime tip, the relationship part of Nhibernate is different from what Entity Framework (so much Core and v.6) and the keys to relationship:

public int PostId { get; set; } 
public Post Post { get; set; }

for example in the Entity Framework are exposed and in Nhibernate no, that is, respectively one is explicit and the other is implicit, out that if not put has the convention that by default puts the name of the fields and creates in the tables or gives error for the lack of these fields, better set up everything.

Browser other questions tagged

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