Blog

Filter posts by the Tags of Blog section!

What's been happened to HasMany() in Entity Framework Core Friday, 21 September 2018

In earlier versions of Entity Framework( before Entity Framework . Core) when you wanted to create an inner table from two Entities, you would be able to use entity framework mapping in a way to create the inner table without creating a separate entity for it. For example, look at the following two classes:

 


 public class Blog

    {

        public int Id { get; set; }

 

        public string Title { get; set; }

 

        public virtual ICollection<Tag> Tags { get; set; }

    }

 

 

  public class Tag

    {

        public int Id { get; set; }

 

        public string Name { get; set; }

    

        public virtual ICollection<Blog> Blogs { get; set; }

 

    }

 

It was possible to create the inner table in the mapping of the above classes:

 

internal class BlogMapping : EntityTypeConfiguration<Blog>

    {

        public BlogMapping()

        {

            ToTable("Blog");

            HasKey(p => p.Id);

            Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Property(p => p.Title).IsRequired().HasMaxLength(400);

        

            HasMany(t => t.Tags).WithMany(p => p.Blogs).Map(m =>

            {

                m.MapLeftKey("BlogId");

                m.MapRightKey("TagId");

                m.ToTable("BlogTag");

            });

        }

    }

 

Now, in Entity Framework Core there is no longer the HasMany() method! It’s not supported anymore (I don't know why and I couldn't find the real reason as I personally would never have trouble with that. See here for more information) and you have to represent the many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships. So, firstly you have to change the Entities like below:

  

 

public class Blog

    {

        public int Id { get; set; }

 

        public string Title { get; set; }

 

        public virtual ICollection<BlogTag> BlogTags { get; set; }

    }

 

 

    public class Tag

    {

        public int Id { get; set; }

 

        public string Name { get; set; }

 

        public virtual ICollection<BlogTag> BlogTags { get; set; }

 

    }

 

    public class BlogTag

    {

        public int BlogId { get; set; }

 

        public Blog Blog { get; set; }

 

        public int TagId { get; set; }

 

        public Tag Tag { get; set; }

    }

 

Secondly, you have to map via Entity Framework Core like this:

  

 public class BlogTagMapping : IEntityTypeConfiguration<BlogTag>

    {

        public void Configure(EntityTypeBuilder<BlogTag> builder)

        {

            builder.HasOne(d => d.Blog).WithMany(e => e.BlogTags).HasForeignKey(w => w.BlogId);

            builder.HasOne(d => d.Tag).WithMany(e => e.BlogTags).HasForeignKey(w => w.TagId);

        }

    }

 

I would have seen a lot of developers writing this sort of mapping for entity framework to create inner tables, but I've hardly ever used this method, and now it seems we all have to use these ways in entity framework core!

comments powered by Disqus