EF Generic Repository get Id from new inserted generic entity

后端 未结 4 1059
感动是毒
感动是毒 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:12

    The solution:

     public override TR Create(T entity)
        {
            string entitySet = GetEntitySetName();
            _context.AddObject(entitySet, entity);
            _context.SaveChanges();
    
            //Returns primaryKey value
            return (TR)context.CreateEntityKey(entitySet, entity).EntityKeyValues[0].Value;                       
        }
    

    Where T: entity type, TR: entity PK type.

提交回复
热议问题