开发者

How to write generic Entity name in constructor in EF4?

i try to write Generic Repository in EF 4. But my codes is not looking good. Because ExpressionEntities is not GENERIC.

I want to convert this codes:

public class DataRepository<T> : IRepository<T> where T : class
{
    private ExpressionsEntities 开发者_JAVA技巧_context;
    public DataRepository()
    {
    }

    public DataRepository(ExpressionsEntities context)
    {
        _context = context;
    }
}

to the following:

public class DataRepository<T> : IRepository<T> where T : class
{
    private GetGenericEntityCONTEXT _context;

    public DataRepository()
    {
    }

    public DataRepository(GetGenericEntityCONTEXT  context)
    {
        _context = context;
    }
}

because ExpressionsEntities not global my entities : ExpressionsEntities1, ExpressionsEntities 2, ExpressionsEntities 3 etx... i need to write get take entity for example:

public class DataRepository<T> : IRepository<T> where T : class
{
    private Entity _context;
    public DataRepository()
    {
    }

    public DataRepository(Entity context)
    {
        _context = context;
    }

    public class Main
    {
        main()
        {
            new DataRepository(ExpressionEntities)
        }
    }
}


Not 100% sure what your asking - from what i can understand, you wan't a way to dynamically create the entity set based on T.

Well that's easy enough:

public class DataRepository<T> : IRepository<T> where T : class
{
   private ObjectContext _ctx; 

   public DataRepository<T>(ObjectContext ctx)
   {
      this._ctx = ctx;
   }

   public IObjectSet<T> CurrentEntitySet<T>()
   {
       get
       {
          var entityName = _plularizer.Pluralize(typeof(T).Name);
          string entitySetName = string.Format("{0}.{1}", EntityContainerName, entityName);
          return _ctx.CreateObjectSet<T>(entitySetName );  
       }
   }
}

Then your specific Repository could do this (for example):

public class AppleRepository : DataRepository<Apple>
{
   public AppleRepository(IObjectContext ctx) : base(ctx) {}

   public ICollection<Apple> FindApples(Func<Apple,bool> predicate)
   {
      return CurrentEntitySet.Where(predicate).ToList();
   }
}

And when your creating your repository, pass through the object context - preferably by DI:

var repository = new AppleRepository(new ExpressionEntities()); // should be DI'ed

Basically, we're making use of Pluralization (the same code used by Entity Framework to pluralize entity set names), and CreateObjectSet<T>.

So if you created a DataRepository<Apple>, that would translate to an entity set name of Apples (which should match the entity set on your model), and we create an entity set based on that.

The EntityContainerName property is what's on your EDMX - you should pass this through the ctor (via DI preferably).

Does that answer your question?


I prefer to get the entityName like this.

EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);

EntitySetBase entitySet = container.BaseEntitySets.Where(item => item.ElementType.Name.Equals(typeof(T).Name)).FirstOrDefault();

var entityName = entitySet.Name

This deals with some cases if you have some classes pluralized, and some classes not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜