规格: MySQL.Data 8.0.11 .Net 4.6 EF 6.2 使用ASP身份
案例1
这是我试图实现的,我已经在Auto Mapper中将DTO映射到了正确的表格。我在asp.net Identity创建的“Manage”控制器中添加了这段代码。这没有用,所以我试着用案例2进行试验。现在这个代码确实返回了数据,当我试图将这个代码粘贴到另一个不直接相关的控制器中时。
public IEnumerable<GetAddressDto> GetAllAddresses()
{
//var user = UserManager.FindById(User.Identity.GetUserId());
//var customer = _context.Customer.SingleOrDefault(c => c.ApplicationUser.Id == user.Id);
var Obj = (from custaddr in _context.CustomerAddress
where custaddr.Customer.CustomerId == 1
select new GetAddressDto
{
AddressId = custaddr.AddressId,
AddressLine1 = custaddr.AddressLine1,
AddressLine2 = custaddr.AddressLine2,
Circle = custaddr.Circle,
City = custaddr.City,
Landmark = custaddr.Landmark,
IsPrimaryAddress = custaddr.IsPrimaryAddress,
Pincode = custaddr.Pincode,
IsActive = custaddr.IsActive,
IsDeleted = custaddr.IsDeleted,
IsDev = custaddr.IsDev
});
var Obj2 = _context.CustomerAddress.ToList();
return Obj;
}
输出
SELECT 1 AS [C1], [Extent1].[AddressId] AS [AddressId], [Extent1].[AddressLine1] AS [AddressLine1], [Extent1].[AddressLine2] AS [AddressLine2], [Extent1].[Circle] AS [Circle], [Extent1].[City] AS [City], [Extent1].[Landmark] AS [Landmark], [Extent1].[IsPrimaryAddress] AS [IsPrimaryAddress], [Extent1].[Pincode] AS [Pincode], [Extent1].[IsActive] AS [IsActive], [Extent1].[IsDeleted] AS [IsDeleted], [Extent1].[IsDev] AS [IsDev] FROM [dbo].[CustomerAddresses] AS [Extent1] WHERE 1 = [Extent1].[Customer_CustomerId]
案例2:
这是另一个实验,当DTO方式无效时,我试图做的事情。
public List<CustomerAddress> GetAllAddresses() {
//var user = UserManager.FindById(User.Identity.GetUserId());
//var customer = _context.Customer.SingleOrDefault(c => c.ApplicationUser.Id == user.Id);
/*var Obj = (from custaddr in _context.CustomerAddress
//where custaddr.Customer.CustomerId == 1
select new GetAddressDto
{
AddressId = custaddr.AddressId,
AddressLine1 = custaddr.AddressLine1,
AddressLine2 = custaddr.AddressLine2,
Circle = custaddr.Circle,
City = custaddr.City,
Landmark = custaddr.Landmark,
IsPrimaryAddress = custaddr.IsPrimaryAddress,
Pincode = custaddr.Pincode,
IsActive = custaddr.IsActive,
IsDeleted = custaddr.IsDeleted,
IsDev = custaddr.IsDev
});*/
var Obj2 = _context.CustomerAddress.ToList();
return Obj2;
}
输出
System.Collections.Generic.List`1[sp_pocketefarms_web.Models.CustomerAddress]
我无法理解为什么会这样,请帮忙。