1

In reference to the selected answer in How can I get Id of inserted entity in Entity Framework? work for an Identity field that is a Guid?

Also, is there a way to just do a simple `SELECT @@IDENTITY' in a repository and interface setting?

Edit 1

I need to be able insert a record using the generic Repo method.

My model:

[Table(name: "Images")]
public class ImagesViewModel
{
    [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid RecordID { get; set; }

    [Column(TypeName = "image")]
    public byte[] MainImage { get; set; }

    public string MainImageMimeType { get; set; }

    [Column(TypeName = "image")]
    public byte[] ImageThumbnail { get; set; }

    public string ImageThumbnailMimeType { get; set; }

    public bool Enabled { get; set; }

    public bool PrimaryImage { get; set; }

    public bool RelatedImage { get; set; }

    public Guid? RelatedImageID { get; set; }

    public ImagesViewModel()
    {
        Enabled = true;
        PrimaryImage = true;
        RelatedImage = false;
    }
}

If I was doing a stored procedure, I could simply do a SELECT @@IDENTITY and return the value of the identity field or the newly inserted record.

Unfortunately, I am using code-first with a generic data repository. So far I have the following method (work in progress):

public virtual Guid Add<T>(T newItem) where T : class
{
    using (var context = new ApplicationDbContext())
    {
        context.Set<T>().Add(newItem);
        context.SaveChanges();

        context.Entry(newItem).

        return _ImagesViewModel.RecordID;
    };
}

So I guess I am asking, what is the proper way of doing this?

Edit 2

Here is an updated code snippet of the Add method (work in progress):

public virtual Guid Add<T>(T entity) where T : BaseEntity
{
    using (var context = new ApplicationDbContext())
    {
        DbSet dbSet = context.Set<T>();
        dbSet.Add(entity);
        context.SaveChanges();
        return entity.RecordID;
    }
}

public abstract class BaseEntity
{
    Guid RecordID { get; set; }
}
Community
  • 1
  • 1
John Schultz
  • 672
  • 1
  • 10
  • 29
  • Your question is not clear (at least for me).Can you provide more info ? – Sampath Aug 20 '16 at 16:01
  • SQL Server can **only** use `int` (or `bigint`) for `identity` columns - there is no such things as a "GUID identity".... and as a corollary - **no** that mechanism for `identity` does **not** work with GUIDs ... – marc_s Aug 20 '16 at 16:20
  • OK, I understand that, however is there a way to return the Guid ID for a newly created record in eF6? – John Schultz Aug 20 '16 at 16:24
  • The id will be updated automatically by EF once SaveChanges is called. As entity being reference type, you will get the updated id in the entity in your calling layer itself. You dont have to pass it from generic repository. EDIT : just noticed that you are having a new context in your generic repo; Im not sure whether this is the way repository pattern is to be implemented. IMO, the context should be same in the request scope to leverage the power of unit of work. – Developer Aug 20 '16 at 17:30

2 Answers2

2

I would like to suggest below mentioned implementation to you.I don't know whether is it possible to do it on your side or not.

public virtual Guid Add<T>(T entity) where T : BaseEntity
    {
        using (var context = new ApplicationDbContext())
        {
            DbSet dbSet = context.Set<T>();
            dbSet.Add(entity);
            context.SaveChanges();
            return entity.Id;
        }
    }

public abstract class BaseEntity
{
   public Guid Id { get; set;}
}
Sampath
  • 63,341
  • 64
  • 307
  • 441
0

I was able to get it working. By changing the code in my BLL to just return the ID after the add as follows:

Change void Users_Add(SiteUsersModel _SiteUsersModel); to Guid Users_Add(SiteUsersModel _SiteUsersModel);

and

public void Users_Add(SiteUsersModel _SiteUsersModel)
{
    _IUsersRepository.Add(_SiteUsersModel);
}

to

public Guid Users_Add(SiteUsersModel _SiteUsersModel)
{
    Guid RecordID = new Guid();
    using (var context = new ApplicationDbContext())
    {
        context.SiteUsers.Add(_SiteUsersModel);
        context.SaveChanges();
        RecordID = _SiteUsersModel.RecordID;
    }
    return RecordID;
}

It seems to be good to go.

John Schultz
  • 672
  • 1
  • 10
  • 29