我是ASP.Net Core的新手。通常我从存储库发送boolean来执行Insert,Update或Delete的存储过程:
public bool AddProduct(ProductInsertModel model)
{
bool DoAddProduct()
{
try
{
using (var connection = _connectionManager.GetOpenConnection())
{
return connection.QueryFirstOrDefault<bool>("Product.usp_Product_Create",
param: model,
commandType: CommandType.StoredProcedure);
}
}
catch (Exception ex)
{
return false;
}
}
return DoAddProduct();
}
ProductInsert
模型(从存储库发送):
public class ProductInsertModel
{
public string Name { get; set; }
public int BrandId { get; set; }
public int SubcategoryId { get; set; }
public int LanguageId { get; set; }
public string ProductDetailName { get; set; }
public string Description { get; set; }
public string KeepMeSafeDescription { get; set; }
public bool IsOneOfAKind { get; set; }
public int Waist { get; set; }
public int Hip { get; set; }
public int Chest { get; set; }
public int SkirtLong { get; set; }
public decimal Price { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModifiedDate { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public bool IsDeleted { get; set; }
}
然后我使用ProductService
来调用存储库,如下所示:
public bool Insert(ProductInsertModel model)
{
bool DoInsert()
{
return _repo.AddProduct(model);
}
return DoInsert();
}
最后在控制器中,我调用了ProductService
:
public IActionResult Insert([FromBody]ProductInsertModel model)
{
IActionResult DoInsert()
{
try
{
var rModel = _productService.Insert(model);
return Ok(rModel);
}
catch (Exception ex)
{
return StatusCode(500, ex);
}
}
return DoInsert();
}
型号:
public class ProductInsertModel
{
public int ProductId { get; set; }
public string Name { get; set; }
public int ProductDetailId { get; set; }
public int BrandId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public int LanguageId { get; set; }
public string ProductDetailName { get; set; }
public string Description { get; set; }
public string KeepMeSafeDescription { get; set; }
public bool IsOneOfAKind { get; set; }
public int Waist { get; set; }
public int Hip { get; set; }
public int Chest { get; set; }
public int SkirtLong { get; set; }
public decimal Price { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModifiedDate { get; set; }
public string CreatedBy { get; set; }
public string ModifiedBy { get; set; }
public bool IsDeleted { get; set; }
public int ProductPackageId { get; set; }
public int PackageId { get; set; }
}
存储过程:
ALTER PROCEDURE [Product].[usp_Product_Create]
@Name varchar(255) ,
@BrandId int,
@SubcategoryId int,
@LanguageId int,
@ProductDetailName varchar(255),
@Description varchar(255),
@KeepMeSafeDescription varchar(255),
@IsOneOfAKind bit,
@Waist int,
@Hip int,
@Chest int,
@SkirtLong int,
@Price decimal(19,2),
@CreationDate datetime,
@ModifiedDate datetime,
@CreatedBy varchar(50),
@ModifiedBy varchar(50),
@IsDeleted bit
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Product.Product
VALUES (@Name, @BrandId, @SubcategoryId, @IsOneOfAKind);
INSERT INTO Product.ProductDetail
VALUES (IDENT_CURRENT('Product.Product'), @LanguageId, @ProductDetailName,
@Description, @KeepMeSafeDescription, @Waist, @Hip, @Chest,
@SkirtLong, @Price, @CreationDate, @ModifiedDate,
@CreatedBy, @ModifiedBy, @IsDeleted);
COMMIT TRANSACTION
RETURN 1;
END TRY
BEGIN CATCH
EXEC usp_Sql_Error_Codes 'usp_Product_Create'
ROLLBACK TRANSACTION
RETURN 0
END CATCH
END
它工作得很好,现在我想知道我需要做什么来替换为简单的get动作添加Product并从SP接收项目到C#对象。我有Get
存储过程,如下所示:
ALTER PROCEDURE [User].[usp_Get_UserProfile]
@UserId INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION
SELECT
MCU.UserName, MCU.FirstName, MCU.MaidenName, MCU.LastName,
MCU.Age, MCU.Avatar, MCU.Email, MCU.EmailConfirmed,
MCU.PhoneNumber, MCU.ProfilePicture, MCU.UserName
FROM
SecurityMc.McUsers AS MCU
WHERE
MCU.McUserId = @UserId
COMMIT TRANSACTION
END TRY
BEGIN CATCH
EXEC usp_Sql_Error_Codes 'usp_Get_UserProfile'
ROLLBACK TRANSACTION
END CATCH
END
答案 0 :(得分:0)
在Service中编写如下方法来调用SP并填充模型: -
public IList<UserProfileModel> GetUserDetails(int UserId)
{
using (var connection = _connectionManager.GetOpenConnection())
{
List<UserProfileModel> data = connection.SelectMany<UserProfileModel>("User.usp_Get_UserProfile",
param: UserId,
commandType: CommandType.StoredProcedure);
return data;
}
在表示层中创建/继承类似的模型UserProfileViewModel。