2

I had implemented Fluent API using Entity Framework 6. Have problems when implementing the same using EnityFrameworkCore.

Below is the code using Fluent API using EntityFramework 6

 public class CustomerConfiguration : EntityTypeConfiguration<Customers>
    {
        public CustomerConfiguration()
        {
            ToTable("Customers");
            Property(c => c.FirstName).IsRequired().HasMaxLength(50);
            Property(c => c.LastName).IsRequired().HasMaxLength(50);
            Property(c => c.Gender).IsRequired().HasMaxLength(10);
            Property(c => c.Email).IsRequired().HasMaxLength(25);
            Property(c => c.Address).IsRequired().HasMaxLength(50);
            Property(c => c.City).IsRequired().HasMaxLength(25);
            Property(c => c.State).IsOptional().HasMaxLength(15);

        }
    }


  protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CustomerConfiguration());
            modelBuilder.Configurations.Add(new OrderConfiguration());
            modelBuilder.Configurations.Add(new ProductConfiguration());

            modelBuilder.Entity<Orders>()
           .HasRequired(c => c.Customers)
           .WithMany(o => o.Orders)
           .HasForeignKey(f => f.CustomerId);

            modelBuilder.Entity<Orders>()
                .HasMany<Products>(s => s.Products)
                .WithMany(c => c.Orders)
                .Map(cs =>
                {
                    cs.MapLeftKey("OrderRefId");
                    cs.MapRightKey("ProductRefId");
                    cs.ToTable("OrderDetails");
                });


        }

The issues that I am getting in EntityFrameworkCore are

  1. It dosent recognize ToTable and Property keywords in CustomerConfiguration()
  2. It dosent recognize Configurations, HasRequired,MapLeftKey,MapRightKey,ToTable keyword in OnModelCreating method

Could somebody tell me how to achieve this using Fluent API in EntityFrameWork Core

1

2 Answers 2

5

EF core uses completely different APIs.So you must learn it first.

As an example : This is the way how it sets the ToTable().

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .ToTable("blogs");
    }

To learn you have to read these links :

Table Mapping

Relationships

Creating a Model

To encapsulate the configuration for an entity type in a class :

using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Microsoft.EntityFrameworkCore
{
    public abstract class EntityTypeConfiguration<TEntity>
        where TEntity : class
    {
        public abstract void Map(EntityTypeBuilder<TEntity> builder);
    }

    public static class ModelBuilderExtensions
    {
        public static void AddConfiguration<TEntity>(this ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration)
            where TEntity : class
        {
            configuration.Map(modelBuilder.Entity<TEntity>());
        }
    }
}

You can see more details here (see the first post which was edited by @rowanmiller : Github

Sign up to request clarification or add additional context in comments.

7 Comments

So I cant create seperate configuration files and they add the configuration in onmodelcreating
you can refer this link.please let me know whether is that working or not ? stackoverflow.com/questions/34855989/…
As far as I can see you can't. The issue for EntityTypeConfiguration<T> still open: github.com/aspnet/EntityFramework/issues/2805
Thanks for the link.EF team has given workaround for that.please see my post @BotondBalázs
Looks great! I don't understand why they haven't included it in the framework itself.
|
0

direct link to table maping documentation. Read the rest of it as well. EF core is a new product not an upgrade to EF6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.