EF Generic Repository get Id from new inserted generic entity

后端 未结 4 1061
感动是毒
感动是毒 2020-12-19 03:30

All my entities has property Id. All my tables in database has auto-generated integer identity Id as primary key.

I have generic method to Create entities.

I

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 04:16

     With reflection you can obtain the Id property.
    
    
    public override int Create(T entity)
        {
            string entitySet = GetEntitySetName();
            _context.AddObject(entitySet, entity);
            _context.SaveChanges();
    
             var idProperty = item.GetType().GetProperty("Id").GetValue(entity,null);
             return (int)idProperty;
        }
    

提交回复
热议问题