andrebaltieri / Flunt

Validations and Notifications
https://github.com/andrebaltieri/flunt
MIT License
624 stars 162 forks source link

Mapear Entidade implementa Notifiable #48

Closed vitormoschetta closed 3 years ago

vitormoschetta commented 4 years ago

Boa noite.

Sou um pouco inexperiente.

Ao tentar mapear, no DbContext (EF Core), uma classe que Herda/Implementa Notifiable, recebo a seguinte notificação:

The entity type 'Notification' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.

Nas configurações do DbSet então adiciono:

modelBuilder.Entity<Notifiable>().HasNoKey();

E então recebo outra notificação:

The entity type 'Cliente' cannot be mapped to a table because it is derived from 'Notifiable'. Only base entity types can be mapped to a table.

Como você faz para resolver essa questão ao usar o EF Core?

eduprog commented 4 years ago

@vitormoschetta seu problema não é flunt e sim sua entidade, que o ef está dizendo que sua classe não tem uma chave primária. Posta o código concreto da sua classe e o como você mapeou para o EF.

modelBuilder.Entity().HasNoKey() esta config não tem porque.

vitormoschetta commented 4 years ago

Notification não deve ser mapeado, a solução que encontrei foi:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Ignore<Notification>();
    base.OnModelCreating(modelBuilder);
}

Como existe uma composição entre Notifiable e Notification, quando minha entidade herda Notifiable o EF Core Tenta Mapear Notification e gerar a tabela.

vitormoschetta commented 3 years ago

Precisei implementar com Dapper também, em um projeto Win Forms. Não consegui Ignorar as propriedades do Flunt no Dapper.Contrib.Extensions;

Solução: Decorar as propriedades 'Notifications', 'Invalid', e 'Valid' da classe Notifiable com a anotação [Computed]

Ex:


using Dapper.Contrib.Extensions;

public abstract class Notifiable
{
   [Computed]
   public IReadOnlyCollection<Notification> Notifications => _notifications;

   [Computed]
   public bool Invalid => _notifications.Any();

   [Computed]
   public bool Valid => !Invalid;
}
andrebaltieri commented 3 years ago

@vitormoschetta I cant do that! Cant add reference to System.ComponentModels.DataAnnotations to Flunt. The notifications are not for ORM/Database related only... I use it a lot on my Console Apps for example.

We can discuss a way to solve this without evolving annotations. Did you have more details on what happened to provide us? We can work on this together!

andrebaltieri commented 3 years ago

Since we're making contract generic, you can move your notifications outside your entity. Also you can extend and create custom notifications or even remove mapping from Notifications property.

It's more an ORM related issue... closing this.