Search code examples
entity-frameworkasp.net-mvc-4fluent-nhibernatefluent-nhibernate-mapping

Can a mapping class be created in Entity Framework similar to this one in Fluent NHibernate?


I want to create a mapping class similar to the one mentioned below. I want to convert this Fluent NHibernate mapping class to Entity Framework.

Fluent NHibernate

using FluentNHibernate.Mapping;

using MyBlog.Core.Objects;


public class PostMap: ClassMap<Post>
{
    public PostMap()
    {
        Id(x => x.Id);

        Map(x => x.Title)
            .Length(500)
            .Not.Nullable();

        Map(x => x.ShortDescription)
            .Length(5000)
            .Not.Nullable();

        Map(x => x.Description)
            .Length(5000)
            .Not.Nullable();

        Map(x => x.Meta)
            .Length(1000)
            .Not.Nullable();

        Map(x => x.UrlSlug)
            .Length(200)
            .Not.Nullable();

        Map(x => x.Published)
            .Not.Nullable();

        Map(x => x.PostedOn)
            .Not.Nullable();

        Map(x => x.Modified);

        References(x => x.Category)
            .Column("Category")
            .Not.Nullable();

        HasManyToMany(x => x.Tags)
            .Table("PostTagMap");
    }
}

Is NHibernate support available with Hosting Services? Is it easily available with any ASP.NET Hosting or only selected services use it?


Solution

  • Yes, Entity Framework has the similar mappings.

    NHibernate:

    public PostMap()
    {
        Map(x => x.Title)
            .Length(500)
            .Not.Nullable();
    }
    

    Entity Framework:

    public class YourDomainModelContext : DbContext
    {
        public YourDomainModelContext() { }
        ...
        public DbSet<Post> Posts { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Post>()
                .Property(u => u.Title)
                .HasMaxLength(500);
        }
    }
    

    You can get more information in these blog-posts: