Entity Framework learning intermediate piece 4, storage process

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

In EF, each entity insert, update and delete all through the use of stored procedures to complete, in order to improve performance. This is similar to the data set. The steps are: first, the definition of a stored procedure, and then in the visual designer VS, stored procedures can be set mapping.

Here, thought the Supplier entity mapping storage process as an example.

Each insert, update and delete the stored procedure.

InsertSuppliers stored procedures are defined as follows:

CREATE PROCEDURE [dbo].[InsertSuppliers]

-- Add the parameters for the stored procedure here

@CompanyName nvarchar(40),

@ContactName nvarchar(30),

@ContactTitle nvarchar(30),

@Address nvarchar(60),

@City nvarchar(15),

@Region nvarchar(15),

@PostalCode nvarchar(10),

@Country nvarchar(15),

@Phone nvarchar(24),

@Fax nvarchar(24),

@HomePage ntext

AS

BEGIN

-- interfering with SELECT statements.

SET NOCOUNT ON;

INSERT INTO Suppliers(CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,HomePage)

VALUES(@CompanyName,@ContactName,@ContactTitle,@Address,@City,@Region,@PostalCode,@Country,@Phone,@Fax,@HomePage);

SELECT SCOPE_IDENTITY() AS SupplierID;

END

DeleteSuppliers stored procedures are defined as follows:

CREATE PROCEDURE [dbo].[DeleteSuppliers]

-- Add the parameters for the stored procedure here

@SupplierID int

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

DELETE Suppliers WHERE SupplierID=@SupplierID

END

UpdateSuppliers stored procedures are defined as follows:

CREATE PROCEDURE [dbo].[UpdateSuppliers]

 -- Add the parameters for the stored procedure here

@SupplierID int,

@CompanyName nvarchar(40),

@ContactName nvarchar(30),

@ContactTitle nvarchar(30),

@Address nvarchar(60),

@City nvarchar(15),

@Region nvarchar(15),

@PostalCode nvarchar(10),

@Country nvarchar(15),

@Phone nvarchar(24),

@Fax nvarchar(24),

@HomePage ntext

AS

BEGIN

 -- SET NOCOUNT ON added to prevent extra result sets from

 -- interfering with SELECT statements.

 SET NOCOUNT ON;

UPDATE Suppliers SET CompanyName=@CompanyName,ContactName=@ContactName,ContactTitle=@ContactTitle

,Address=@Address,City=@City,Region=@Region,PostalCode=@PostalCode,Country=@Country,Phone=@Phone,Fax=@Fax,HomePage=@HomePage 

 WHERE SupplierID=@SupplierID

END


Then, in the VS visual designer, open the entity model edmx file, and then in the Suppliers entity the right mouse button à “ storage process mapping ”, then set the stored procedure in the. As shown below:  

So far, the stored procedure mapping. We come to specific use, the code as follows:

Using the stored procedure Insert:

[Test]

public void SPInsert()

{

using (var db = new NorthwindEntities1())

{

var supplier = new Suppliers();

supplier.CompanyName = "cnblogs";

supplier.ContactName = "xray2005";

supplier.ContactTitle = "net";

supplier.City = "Chengdu";

supplier.Region = "Sichuan";

supplier.Country = "China";

supplier.PostalCode = "600041";

supplier.Phone = "028-8001";

supplier.Fax = "028-8002";

supplier.HomePage = "http://xray2005.cnblogs.com";

db.AddToSuppliers(supplier);

db.SaveChanges();


}

}

Using the stored procedure Update:

[Test]

public void SPUpdate()

{

using (var db = new NorthwindEntities1())

{

var supplier = db.Suppliers.FirstOrDefault(s => s.SupplierID == 30);

Assert.IsNotNull(supplier);

supplier.CompanyName = "CNBLOGS";

supplier.ContactName = "xray2005";

supplier.ContactTitle = "♂The wind mill.Net";

supplier.City = "Chengdu";

supplier.Region = "

Sichuan";

supplier.Country = "China";

supplier.PostalCode = "600040";

supplier.Phone = "028-1008611";

supplier.Fax = "028-10086";

supplier.HomePage = "http://www.cnblogs.com/xray2005";

db.SaveChanges();


}

}

Using the stored procedure Delete:

[Test]

public void SPDelete()

{

using (var db = new NorthwindEntities1())

{

var supplier = db.Suppliers.FirstOrDefault(s => s.SupplierID == 31);

Assert.IsNotNull(supplier);

db.DeleteObject(supplier);

db.SaveChanges();

var supplier1 = db.Suppliers.FirstOrDefault(s => s.SupplierID == 31);

Assert.IsNull(supplier1);

}

}

So far, the entity store process mapping are introduced. In this section, the content is relatively simple.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Avivi at November 14, 2013 - 10:39 AM