代码之家  ›  专栏  ›  技术社区  ›  Paul

链接MapHierarchy()以首先在EF代码中添加更多案例

  •  1
  • Paul  · 技术社区  · 15 年前

    我希望会有一个疯狂的林肯的答案,感觉应该有

    当前代码:

    MapHierarchy().Case<Folder>(f => new { FolderId = f.Id, 
                                            f.Name,
                                            Type = 1 })
                          .Case<RootFolder>(f => new { f.RootName,
                                                        Type = 2 })
                          .ToTable("Folder");
    

    另一种方法:

    void AddAnotherFolder(something)
    {
       something.Case<RootFolder>(f => new { f.RootName, Type = 2 })
    }
    

    我想做的是将该方法传递到MapHierarchy字符串中—这可能吗?

    有点像。。。

    MapHierarchy().Case<Folder>(f => new { FolderId = f.Id, 
                                            f.Name,
                                            Type = 1 })
                          .Case<RootFolder>(f => new { f.RootName,
                                                        Type = 2 })
                          .AddAnotherFolder(this)
                          .ToTable("Folder");
    

    感觉像是临场提问,如果可以的话会很酷的

    1 回复  |  直到 15 年前
        1
  •  1
  •   Ian Mercer    15 年前

    查看的方法签名 Case<T> . 您应该能够在静态类中创建一个类似的扩展方法,其中包含特定类型的where子句。可能是这样的:

    public static class ExtensionClass
    {
        public static HierarchyEntityMap<TEntity> AddAnotherFolder<TEntity>(this HierarchyEntityMapGenerator<TEntity> source) where TEntity:RootFolder
        {
           return source.Case<TEntity>(f => new { f.RootName, Type = 2 });
        }
    }
    

    你就可以这么说 .AddAnotherFolder() 没有这个。

    [没有测试,但会是这样]