1

I have an InsertPerson stored procedure and i need it to return a GUID (Uniqueidentifier) into my person object that is being created. Is this even possible with entity framework codefirst? I've tried everything and entity framework is ignoring the guid im trying to return using an output parameter. Does anyone have an example if this is possible?

Person.cs:

public class Person
{
   public Guid Id { get; set; }
   public string FirstName { get; set; }
}

Stored Procedure:

CREATE PROCEDURE [dbo].[InsertPerson]
    @KeyPlayerId UNIQUEIDENTIFIER ,
    @FirstNameNVARCHAR(255) 
AS

-- Perform Insert
insert into [dbo.].[Person]....

-- Return GUID
select @Id as [Id];

END;
user2309367
  • 317
  • 2
  • 8
  • What does your SP look like and how are you calling it? – James Jan 03 '14 at 18:29
  • how you're calling the stored procedure? – Guillermo Oramas R. Jan 03 '14 at 20:43
  • Do you want to map this stored proc to the Create operation for this entity? If this is the case you would have to use EF6 which supports mapping CUD operation to stored procs in CodeFirst apps - http://msdn.microsoft.com/en-us/data/dn468673. – Pawel Jan 03 '14 at 23:32

2 Answers2

1

I was able to accomplish it by telling Entity Framework that the Id is a Database Generated value. I mapped the Insert procedure to InsertPerson in the PersonMap class and use that when the model is created in the OnModelCreating method. In the stored procedure I generate a new Id and pass this back to Entity Framework. Hope this helps!

PersonMap.cs

public class PersonMap : EntityTypeConfiguration<Person>
{
    public PersonMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);

        // Tell Entity Framework the database will generate the key.
        this.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        // Properties
        this.Property(t => t.FirstName)
            .IsRequired()
            .HasMaxLength(255);

        //Map to Stored Procedure
        this.MapToStoredProcedures(s => s.Insert(i => i.HasName("InsertPerson")));
    }
}

OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new PersonMap());            
    }

InsertPerson Stored Procedure

CREATE PROCEDURE InsertPerson 
-- Since Id is marked as Database Generated we only need 
-- a parameter for First Name
@FirstName nvarchar(255) = 0 
AS
-- Variable to hold new Id
DECLARE @Id uniqueidentifier

-- Generate a new Id using NEWID function that returns a unique identifier
SET @Id = NEWID()

-- Perform Insert
INSERT INTO [dbo].[Person] VALUES (@Id, @FirstName)

-- Return the Id to Entity Framework
SELECT @Id AS 'Id'
mperry
  • 91
  • 5
0

I think this may help you:

var sqlConnection1 = new SqlConnection("Your Connection String");
var cmd = new SqlCommand();
var reader;

cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
rdr = cmd.ExecuteReader();
var outputValue = Guid.Parse(cmd.Parameters["@Response"].Value.ToString());

sqlConnection1.Close();
Guillermo Oramas R.
  • 1,303
  • 2
  • 14
  • 31