Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modelBuilder.Configurations.Add and modelBuilder.Entity on OnModelCreating

I have just started working on entity framework code first approach, I have written two approaches below and both are working fine.

Please let me know what are the core concepts behind both these approaches and what should follow?

Approach 1 : Using EntityTypeConfiguration

public class BlogsMap : EntityTypeConfiguration<Blog>
    {
        public BlogsMap(string schema)
        {
            ToTable("BLOG");
            HasKey(t => t.BlogId);
            Property(t => t.BlogId).HasColumnName("BLOGID");
            Property(t => t.Name).HasColumnName("NAME");
            Property(t => t.Url).HasColumnName("URL");
        }

    }


public class BlogContext : DbContext
    {
        public BlogContext(string name)
            : base(name)
        {
        }

        public IDbSet<Blog> BLOG { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           modelBuilder.Configurations.Add(new BlogMap(string.Empty));
        }
    }

Approach 2 :

public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public virtual List<Post> Posts { get; set; }
    }


    public class BloggingContext : DbContext
    {     

        public DbSet<Blog> Blogs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Blog>();

        }
    }

Please provide me concept/blogs on entity,since i have just started.

like image 692
Dev Avatar asked Jan 02 '15 19:01

Dev


People also ask

What is ModelBuilder in Entity Framework?

The ModelBuilder is the class which is responsible for building the Model. The ModelBuilder builds the initial model from the entity classes that have DbSet Property in the context class, that we derive from the DbContext class. It then uses the conventions to create primary keys, Foreign keys, relationships etc.

What is entity type configuration?

Allows configuration to be performed for an entity type in a model. An EntityTypeConfiguration can be obtained via the Entity method on DbModelBuilder or a custom type derived from EntityTypeConfiguration can be registered via the Configurations property on DbModelBuilder.

Which method is used to apply configuration to entities or their properties in the Entity Framework Core?

Property Mapping. The Property method is used to configure attributes for each property belonging to an entity or complex type.

How do you configure a primary key for an entity in Entity Framework fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.


1 Answers

You have several ways to configure your entities. Below I will show three ways,one using DataAnnotations and two using Fluent Api.

The first variant is using DataAnnotations. You can use attributes(DataAnnotations) to configure your entity classes and properties.DataAnnotations attributes overrides default Code First conventions:

[Table(“BLOGS”)]
public class Blog
{
    [Key]
    [Column(“BLOGID”)]
    public int BlogId { get; set; }
    [Column(“NAME”)]
    public string Name { get; set; }
    [Column(“URL”)]
    public string Url { get; set; }

    public virtual List<Post> Posts { get; set; }
}
[Table(“POSTS”)]
public class Post
{
    [Key]
    [Column(“POSTID”)]
    public int PostId { get; set; }
    [Column(“TEXT”)]
    public string Text { get; set; }

    public int BlogId { get; set; }

    [ForeignKey("BlogId")]
    public virtual BaseCard Blog { get; set; }
}

Then, in your context class, you don’t need to override the OnModelCreating method, EF will use the attribute to map your entities and relationship (it will create a 1-to-many relationship between blog and post):

public class BlogContext : DbContext
{
    public BlogContext(string name)
        : base(name)
    {
    }

    public IDbSet<Blog> Blogs { get; set; }
    public IDbSet<Post> Posts { get; set; }
}  

Configuring with Data Annotations is fairly simple and it may be just what you’re looking for. But Data Annotations only allow you to access a subset of the possible configurations (though much more than you’ve seen so far). The Fluent API , however, gives you access to even more, so you may prefer it for this reason. With Fluent Api you don’t need to use attributes to map your fields and relationships of your entities classes. There are two ways to use Fluent Api:

1-Mapping the entities (fields and relationships) in the OnModelCreating method in your context (Your second Aproach):

public class BloggingContext : DbContext
{     

    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>().ToTable("BLOGS");

        modelBuilder.Entity<Blog>().HasKey(t => t.BlogId);
        modelBuilder.Entity<Blog>().Property(t => t.BlogId).HasColumnName("BLOGID");
        modelBuilder.Entity<Blog>().Property(t => t.Name).HasColumnName("NAME");
        modelBuilder.Entity<Blog>().Property(t => t.Url).HasColumnName("URL");
       // The same with post

        //mapping one-to-many relationship
        modelBuilder.Entity<Post>().HasRequired(c => c.Blog)
       .WithMany(s => s.Posts)
       .HasForeignKey(c => c.BlogId);

}

2-The second variant using Fluent Api is creating mapping classes (Your First Approach). This way, you configure your Entities in classes that inherit of EntityTypeConfiguration<TEntity>:

public class BlogMap : EntityTypeConfiguration<Blog>
{
    public BlogMap()
    {
        ToTable("BLOGS");
        HasKey(t => t.BlogId);
        Property(t => t.BlogId).HasColumnName("BLOGID");
        Property(t => t.Name).HasColumnName("NAME");
        Property(t => t.Url).HasColumnName("URL");
    }

}

public class PostMap : EntityTypeConfiguration<Post>
{
    public PostMap()
    {
        ToTable("POSTS");
        HasKey(t => t.PostId);
        Property(t => t.Text).HasColumnName("TEXT");

      //mapping the relationship
        HasRequired(c => c.Blog)
        .WithMany(s => s.Posts)
        .HasForeignKey(c => c.BlogId);

    }
}

Then, to include the mappings in your context you need to add them in the OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
  modelBuilder.Configurations.Add(new BlogMap());
  modelBuilder.Configurations.Add(new PostMap());
}

The best way to add the configurations is at this way:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
      .Where(type => !String.IsNullOrEmpty(type.Namespace))
      .Where(type => type.BaseType != null && type.BaseType.IsGenericType
           && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
       foreach (var type in typesToRegister)
       {
           dynamic configurationInstance = Activator.CreateInstance(type);
           modelBuilder.Configurations.Add(configurationInstance);
       }
       base.OnModelCreating(modelBuilder);  
}

This last variant (the first approach) for me is the best due to you don’t have to touch your model classes (adding attributes) to specify what you want and is more flexible if you want to add a new entity or change something.

like image 101
octavioccl Avatar answered Oct 08 '22 00:10

octavioccl