简体   繁体   English

可以在实体框架中创建一个类似于Fluent NHibernate中的映射类吗?

[英]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映射类转换为Entity Framework。

Fluent NHibernate 流利的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? 主机服务是否支持NHibernate? Is it easily available with any ASP.NET Hosting or only selected services use it? 是否可以轻松地使用任何ASP.NET主机或只有选定的服务使用它?

Yes, Entity Framework has the similar mappings. 是的,实体框架具有类似的映射。

NHibernate: 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: 您可以在这些博客文章中获得更多信息:

For EF4 and EF5 there are Fluent API generators, but for EF6 not. 对于EF4EF5 ,有Fluent API生成器, 但对于EF6则没有。 I'm also searching for one that works. 我也在寻找有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM