我有一個EF實體,我想使用流暢的語法將其屬性映射到具有函數的DTO。將實體映射到DTO
就拿用戶我能使其工作映射這樣說:
public Task<List<JournalTransactionModel>> GetAllJournalRecords()
{
var journalRecords = db.JournalTransactions
.Include(_ => _.JournalTransactionsAccounts)
.Include(_ => _.User)
.Select(_ => new JournalTransactionModel
{
JournalTransactionId = _.JournalTransactionId,
Date = _.Date,
Description = _.Description,
User = new UserModel
{
UserId = _.UserId,
FirstName = _.User.FirstName,
LastName = _.User.LastName,
FullName = _.User.FirstName + " " + _.User.LastName,
Email = _.User.Email,
UserName = _.User.UserName,
Password = _.User.Password,
UserRoleAndPermissions = new UserRoleModel
{
UserRoleId = _.User.UserRole.UserRoleId,
UserRoleName = _.User.UserRole.UserRoleName,
CanRead = _.User.UserRole.CanRead,
CanWrite = _.User.UserRole.CanWrite
}
},
TransactionAccounts = _.JournalTransactionsAccounts.Select(j => new JournalTransactionAccountModel
{
JournalTransactionAccountId = j.Id,
JournalTransactionId = j.JournalTransactionId,
DebitAccount = j.DebitAccount != null ? new AccountModel
{
AccountId = j.DebitAccount.AccountId,
AccountCategoryName = j.DebitAccount.AccountCategory.AccountCategoryName,
AccountCategoryId = j.DebitAccount.AccountCategoryId,
AccountName = j.DebitAccount.AccountName,
IncreasesWhenDebited = j.DebitAccount.IncreasesWhenDebited
} : null,
CreditAccount = j.CreditAccount != null ? new AccountModel
{
AccountId = j.CreditAccount.AccountId,
AccountCategoryName = j.CreditAccount.AccountCategory.AccountCategoryName,
AccountCategoryId = j.CreditAccount.AccountCategoryId,
AccountName = j.CreditAccount.AccountName,
IncreasesWhenDebited = j.CreditAccount.IncreasesWhenDebited
} : null,
Amount = j.Amount,
Person = j.Person != null ? new PersonModel
{
PersonId = j.PersonId,
FirstName = j.Person.FirstName,
LastName = j.Person.LastName,
FullName = j.Person.FirstName + " " + j.Person.LastName,
Email = j.Person.Email,
SocialSecurityNumber = j.Person.SocialSecurityNumber,
PersonType = new PersonTypeModel
{
Id = j.Person.PeopleType.PeopleTypeId,
Name = j.Person.PeopleType.Name
}
} : null
}).ToList()
}).ToListAsync();
return journalRecords;
}
但是,當我試圖創建一個返回的usermodel我一直得到例外
public Task<List<JournalTransactionModel>> GetAllJournalRecords()
{
var journalRecords = db.JournalTransactions
.Include(_ => _.JournalTransactionsAccounts)
.Include(_ => _.User)
.Select(_ => new JournalTransactionModel
{
JournalTransactionId = _.JournalTransactionId,
Date = _.Date,
Description = _.Description,
User = MapUserToModel(_.User),
TransactionAccounts = _.JournalTransactionsAccounts.Select(j => new JournalTransactionAccountModel
{
JournalTransactionAccountId = j.Id,
JournalTransactionId = j.JournalTransactionId,
DebitAccount = j.DebitAccount != null ? new AccountModel
{
AccountId = j.DebitAccount.AccountId,
AccountCategoryName = j.DebitAccount.AccountCategory.AccountCategoryName,
AccountCategoryId = j.DebitAccount.AccountCategoryId,
AccountName = j.DebitAccount.AccountName,
IncreasesWhenDebited = j.DebitAccount.IncreasesWhenDebited
} : null,
CreditAccount = j.CreditAccount != null ? new AccountModel
{
AccountId = j.CreditAccount.AccountId,
AccountCategoryName = j.CreditAccount.AccountCategory.AccountCategoryName,
AccountCategoryId = j.CreditAccount.AccountCategoryId,
AccountName = j.CreditAccount.AccountName,
IncreasesWhenDebited = j.CreditAccount.IncreasesWhenDebited
} : null,
Amount = j.Amount,
Person = j.Person != null ? new PersonModel
{
PersonId = j.PersonId,
FirstName = j.Person.FirstName,
LastName = j.Person.LastName,
FullName = j.Person.FirstName + " " + j.Person.LastName,
Email = j.Person.Email,
SocialSecurityNumber = j.Person.SocialSecurityNumber,
PersonType = new PersonTypeModel
{
Id = j.Person.PeopleType.PeopleTypeId,
Name = j.Person.PeopleType.Name
}
} : null
}).ToList()
}).ToListAsync();
return journalRecords;
功能那裏有什麼不對?
這是我得到的消息:
「ExceptionMessage」:「LINQ到實體無法識別方法「ACS.Hub.BusinessLogic.Models.UserModel MapUserToModel(ACS.Hub.Repository.User) '方法,並且此方法不能轉換爲商店表達式。「
EF查詢提供程序不支持查詢表達式樹中的自定義方法。如果你想輕鬆定義和重用映射,[AutoMapper](https://github.com/AutoMapper/AutoMapper)包(特別是使用'QueryableExtensions'' ProjectTo')就是爲此。 –