该代码的作用是什么? ASP.NET MVC

时间:2018-07-08 09:00:20

标签: asp.net-mvc

您能解释一下这段代码的作用吗?它是从数据库创建新对象吗?我需要知道该模型如何创建数据库以及如何连接。

public class DictionaryValue : Entity
{
    public string Name { get; set; }
    public string Value { get; set; }        
    public DictionaryType Type { get; set; }
    public string Description { get; set; }
}

internal class DictionaryValueMappings : EntityTypeConfiguration<DictionaryValue>
{
    public DictionaryValueMappings()
    {
        Property(p => p.Name).HasMaxLength(200);
        Property(p => p.Value).IsMaxLength();
        Property(p => p.Description).HasMaxLength(1000);
        Property(p => p.Type).IsRequired();
    }
}

public static IEnumerable<TResult> GetList<TResult>(IPager pager, DictionaryType type, string name = null)
{
    using (var context = Context.Read())
    {
        var query = context.Query<DictionaryValue>().AsQueryable();

        if (!String.IsNullOrEmpty(name))
        {
            query = query.Where(p => p.Name.Contains(name));
        }

        return query
                .Where(p => p.Type == type)
                .OrderBy(p => p.Name)
                .Pager(pager)
                .GetResult<TResult>();
    }
}

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

首先定义一个Dictionaryef类,它告诉ef我的数据库中有一个“ DictionaryValue”表,DictionaryValueMappings类定义了映射类,该类告诉ef上下文每个属性都指向DictionaryValue表中的哪一列,该类将添加到dbContext中。 modelBinder配置对象

最后,GetList方法将返回TResult的列表(通用),该方法使用DictionaryValue表加载自定义设置或数据库中的其他任何内容,要使用此方法,您必须实现GetResult和Pager,以便可以进行加载词典中的各种设置