title: How do you return the user Id with a generic repository method in entity framework?
body: <p>I want to return the key field by doing something like this:</p>
  
  <pre><code>public virtual int InsertWithReturnId(TEntity entity)
          {
  
              dbSet.Add(entity);
              return entity.Id;
          }
  </code></pre>
  
  <p>Many thanks</p>
  
  <p>EDIT #1:</p>
  
  <p>I dont need a save changes... it's done like this:</p>
  
  <pre><code>    public GenericRepository&lt;Group&gt; GroupRepository
              {
                  get
                  {
  
                      if (this.groupRepository == null)
                      {
                          this.groupRepository = new GenericRepository&lt;Group&gt;(context);
                      }
                      return groupRepository;
                  }
              }
  </code></pre>
  
  <p>My pulling (stores successfully the group)</p>
  
  <pre><code>public class GroupMethods
      {
          private UnitOfWork uow;
          public GroupMethods() { this.uow = new UnitOfWork(); }
      public int createGroup(string groupName)
              {
                  Debug.WriteLine(groupName);
                  Debug.WriteLine(HttpContext.Current.User.Identity.GetUserName());
                  ApplicationUser user = ClanMethods.getUser(HttpContext.Current.User.Identity.GetUserName());
  
                  if(groupName != "" &amp;&amp; user != null)
                  {
                      Debug.WriteLine("inside if");
                      Group grp = new Group();
                      grp.creator = user;
                      grp.Members.Add(user);
                      grp.GroupName = groupName;
                      var id = uow.GroupRepository.InsertWithReturnId(grp);
                      uow.Save();
                      Debug.WriteLine("id:"+id);
  
                      Debug.WriteLine("stops here");
                      Debug.WriteLine("grp id:"+grp.GroupId);
                      return id;
                  }
                  return 0;
              }
  }
  </code></pre>
  
  <p>I was hoping I could do grp.GroupId but it keeps failing, so I guess not. Will update on interface in 20 mins</p>
  
  <p>EDIT #3</p>
  
  <p>I am getting:</p>
  
  <pre><code>Warning 1   Type parameter 'TEntity' has the same name as the type parameter from outer type 'gdfinal.DAL.GenericRepository&lt;TEntity&gt;'
  Error   2   The best overloaded method match for 'System.Data.Entity.DbSet&lt;TEntity&gt;.Add(TEntity)' has some invalid arguments
  Error   3   Argument 1: cannot convert from 'TEntity [....\DAL\GenericRepository.cs(16)]' to 'TEntity'  
  Error   4   The type '...chatmodels.Group' cannot be used as type parameter 'TEntity' in the generic type or method '....GenericRepository&lt;TEntity&gt;.InsertWithReturnId&lt;TEntity&gt;(TEntity)'. There is no implicit reference conversion from '....Models.chatmodels.Group' to 'gdfinal.DAL.IIdentifier'.   
  </code></pre>
  
  <p>when using </p>
  
  <pre><code>public virtual int InsertWithReturnId&lt;TEntity&gt;(TEntity entity) where TEntity : IIdentifier
          {
              dbSet.Add(entity);
              return entity.Id;
          }
  </code></pre>
  
  <p>It is successfully inheriting the Interface but really doesnt like it :(</p>