views:

95

answers:

1

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.

Is there any way to get entity Id after entity inserted in to database?

   public override int Create<T>(T entity)
    {
        string entitySet = GetEntitySetName<T>();
        _context.AddObject(entitySet, entity);
        _context.SaveChanges();

        return <Id Here>; //TODO Return Id here
    }

In simple(not generic) repository i may just return Entity.Id, but how to get the same behavior in generic repository? I may have base entity class for all entities which contains int property Id but is there any way to get it work without implementing this inheritance?

+3  A: 

With POCO entities, you'd have to use an interface, reflection, or dynamic.

With EntityObject entities you can read the EntityKey property.

Craig Stuntz
Thanks, Interface is the better way, but temporary i will go with dynamic. 'dynamic currentEntity = entity;' 'return currentEntity.Id;'
bigb