Database Reference
In-Depth Information
Figure 14-3. The Agent table in our database
Listing 14-3. The Agent Model
[Table("Agent", Schema = "Chapter14")]
public class Agent
{
[Key]
[MaxLength(50)]
public string Name { get; set; }
[Required]
[MaxLength(50)]
public string Phone { get; set; }
[Timestamp]
public byte[] TimeStamp { get; set; }
}
You want to use stored procedures to handle the insert, update, and delete actions for the model. These stored
procedures need to be written so that they leverage the optimistic concurrency support provided in Entity Framework.
Do the following to create the stored procedures and map them to actions:
1.
Create the stored procedures in the database using the code in Listing 14-4.
Listing 14-4. Stored Procedures for the Insert, Update, and Delete actions
create procedure Chapter14.InsertAgent
(@Name varchar(50), @Phone varchar(50))
as
begin
insert into Chapter14.Agent(Name, Phone)
output inserted.TimeStamp
values (@Name, @Phone)
end
go
create procedure Chapter14.UpdateAgent
(@Name varchar(50), @Phone varchar(50), @TimeStamp_Original TimeStamp, @RowsAffected int OUTPUT)
as
begin
update Chapter14.Agent set Phone = @Phone where Name = @Name
and TimeStamp = @TimeStamp_Original
set @RowsAffected = @@ROWCOUNT
end
go
 
Search WWH ::




Custom Search