generic repository EF4 CTP5 getById

前端 未结 2 542
无人共我
无人共我 2020-12-06 08:30

I have a generic repository an I am trying to add a GetById method as shown here C# LINQ to SQL: Refactoring this Generic GetByID method

The problem is my repository

2条回答
  •  甜味超标
    2020-12-06 09:24

    try this

        public virtual T GetByID(object id)
        {
    
            // Define the entity key values.
            IEnumerable> entityKeyValues =
                new KeyValuePair[] { 
                new KeyValuePair("Id", id) };
    
            string qualifiedEntitySetName = _context.DefaultContainerName + "." + typeof(T).Name;
            EntityKey key = new EntityKey(qualifiedEntitySetName, entityKeyValues);
    
            return (T)_context.GetObjectByKey(key);           
        }
    

提交回复
热议问题