0
votes

.NET CORE , EntityFrameworkCore


DbContext

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // LIKE THIS

        // dynamically
        Map(); // of Entity method (any Entity what i need)

        // INSTEAD OF

        modelBuilder.Entity<Message>(opt =>
        {
            opt.ToTable("Message");
            opt.HasKey(x => x.Id);
            opt.Property(x => x.AutoId).UseSqlServerIdentityColumn();
            opt.HasAlternateKey(x => x.AutoId);
        });

        modelBuilder.Entity<User>(opt =>
        {
            opt.ToTable("User");
            opt.HasKey(x => x.Id);
            opt.Property(x => x.AutoId).UseSqlServerIdentityColumn();
            opt.HasAlternateKey(x => x.AutoId);
        });

        // x100 Entity more maybe ??
    }
}

.

Base of Entities

public abstract interface IEntity
{
    abstract void Map(ModelBuilder modelBuilder);
}

public abstract class BaseEntity : IEntity
{
    public abstract void Map(ModelBuilder modelBuilder);
}

I have some Entities :

public class Message : BaseEntity
{
    public string Content { get; set; }
    public Guid FromUserId { get; set; }
    public Guid ToUserId { get; set; }


    [ForeignKey("FromUserId")]
    public virtual User FromUser { get; set; }

    [ForeignKey("ToUserId")]
    public virtual User ToUser { get; set; }

    public override void Map(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Message>(opt =>
        {
            opt.ToTable("Message");
            opt.HasKey(x => x.Id);
            opt.Property(x => x.AutoId).UseSqlServerIdentityColumn();
            opt.HasAlternateKey(x => x.AutoId);
        });
    }
}

.

public class User : BaseEntity
{
    public virtual List<Message> ReceivedMessages { get; set; }
    public virtual List<Message> SentMessages { get; set; }

    public override void Map(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>(opt =>
        {
            opt.ToTable("User");
            opt.HasKey(x => x.Id);
            opt.Property(x => x.AutoId).UseSqlServerIdentityColumn();
            opt.HasAlternateKey(x => x.AutoId);
        });
    }
}

How to map an entity's Map method to DataContext's OnModelCreating dynamically.

I use Unit Of Work Design Pattern. I'm creating dbSet to get table's data like this :

private readonly DbContext _dbContext;
//
dbContext.Set<T>(); // T is BaseEntity

When i run Set method dbContext, dbContext's OnModelCreating method is firing.

How can i do ?

1
I forgot that i use .Net Core. There is no property like "Configuration" in EntityFrameworkCore. - canmustu
Sorry, I don’t understand what you are asking. Also, what is BaseEntity? - poke
@poke The post is edited. Please check it out. Especially BaseEntity class and DbContext class. - canmustu
I don’t think you can make this automatically work like that. By the time you are using Set<T>(), the OnModelCreating will already have fired; in fact, it needs to run pretty early in the database context construction, and I don’t think you can add things to it later. – What is wrong with the database context knowing beforehand which entity types it supports? - poke
I edited it again. Look at OnModelCreating method please. That's my problem that i wanna solve. - canmustu

1 Answers

0
votes

Unfortunately, EntityTypeConfiguration is not available in Entity Framework Core as of today. You can read more at GitHub issue log.

Some use EntityTypeBuilder to keep mapping in separate class, but it is not as clean as using EntityTypeConfiguration.

These days, if I have to work with existing database, I just use Code First from database, and keep mapping inside OnModelCreating method like this, until EntityTypeConfiguration is available in EF Core.

You might want to read this similar answer.

enter image description here