Considering blogging data model:
Blog:
Id
Name
Posts
Posts:
Id
BlogId
Date
Blog
BlogDto: Blog
LastPostDate
PostDto: Post
BlogName
The DTOs derived from the entities since all the properties are shared, and the DTO just have some extra ones. Also, based on needs, some different mapping in Automapper are defined:
Post -> PostDto
PostDto -> Post
Post -> Post
PostDto -> PostDto
Blog -> Blog Dto
Blog Dto -> Blog
Blog -> Blog
Blog Dto -> Blog Dto
Then I want to project a query from Post to PostDto (with AutoMapper.EF6) like this:
dbContext.Set<Post>
.Include("Blog")
.AsNoTracking()
.ProjectToListAsync<PostDto>();
But I encounter this error:
"The entity or complex type 'Blog' cannot be constructed in a LINQ to Entities query".
The tip is if I remove the Post -> Post mapping, the error does not appear. I guess because AutoMapper try to map the nested PostDto.Blog navigation property in projection.
Is my approach wrong (DTO inherits from Entity)? If so, I guess the best practice is to define the DTO with repeating all the members inside the Entity, which is bothering!
If not, how can I manage nested mapping here?
Related
I am new to JPA eclipseLink.
I have two entity classes in header and LineItem details level. I can only call by ID in header to fetch the details from LineItem (Using OneToMany and ManyToOne). However, I have different cases where I need to call by name, customer account etc.
I have already tried with all suggestion from different forums but no luck.
I am trying to hide my PasswordHash field from User table by marking it with [JsonIgnore] attribute. localhost/User query gets me a list of users and excludes PasswordHash from the results, but if I write my query as such: localhost/User?$select=PasswordHash, it returns data, even though it should ignore it!!!
I am exposing my Entity Framework table User using an ApiController with [EnableQuery] GetAll(); method, which returns IQueryable<User> as result.
Please help!
I fully agree with the Brian Rogers' suggestion to use a DTO that exposes only necessary fields. It's cheep and secure.
If you decide to keep the existing model, you may look at EDM Security guide which suggests to use the [IgnoreDataMember] attribute.
I am new to EF so please bear with me. I am using Entity Framework with an existing database in which the relationships are not defined.
Using EF, I am able to produce the model, but obviously the 'navigational properties' are not working. Is there a way I can specify the mapping between the entities?
For example, in my Product entity, I have a field CategoryID_fk (maps to Category entity). But since the relationships are not defined, I cannot load a Category while loading a Product entity.
Can someone guide me in this regard?
I do understand that it would be preferable to refactor our database but I am unable to do that now. Thanks in advance.
This link is very useful.http://www.entityframeworktutorial.net/
I think field names are not suitable conventions.Try to change field name as CategoryId or Category_CategoryId .I think it will work
Yes, you can do a group join. Basically you will retrieve Products and Category in separate queries and take an approach as indicated in this answer.
Something along these lines:
var productCategoryList = products.GroupJoin
(
categories,
p=> p.productId,
c=> c.CategoryId,
(p, c) => new ProductCategory
{
//Create your Product Category model here
}
).AsEnumerable();
A general parse.com relationship question really - using swift. I have a blogging app, for which there is a blog class, and a user class (along with a few others!) the blog class stores the associated user ID in a field for simplicity. Can I use includekey (or something similar) in a pfquery for the following;
firstly retrieve specific (or all) blog entries that match a criteria.
for each matching blog entry, check a field in the related user class for an option before returning the JSON list of entries
I suppose, sort of a subquery really, but wanted the whole thing to work in one pfquery if possible.
thanks!
Yes, you can do this with a relational query. The user stored in blog should be a pointer.
First create a query for the field in the user class, i.e.
userQuery.whereKey("age", greaterThan: 30)
(Do not execute this query)
Then, when adding constraints to your blog query, add
blogQuery.whereKey("user", matchesQuery: userQuery)
i'm struggling with LLBLGEN and i guess ORM's in general.
i have created an entity, lets use a library example to explain:
i want to display a book object and also return a list of users who have loaned the book.
so i need to return the book object which contains a list of users.
DTO Book::
int bookId,
string bookName
additionally i wish to return with my book a collection of users who have loaned the book:
List<user> Loans
loans table might look like this:
int id
int userid
int bookid
currently my loans entity has now created this:
DTO Loans
int id
User user // user entity
Book book // book entity
im struggling to understand how this example would work in llblgen. can anyone assist with guidance or point me in the way of a tutorial?
at the moment, when i come up to update my model Book with a new loan associated to a book, im getting stackoverflow errors. i assume this is creating some sort of loop when attempting to update my Book object.
thanks
i noticed when running a profiler on SQL that the sql statement didnt include any join statements onto my relationship entities.
this was because my domain query didnt include prefetch items for my relationships, i added the following to my query:
var query = new DomainSearch<T>
{
SearchText = searchText,
PrefetchItems =
{
new Prefetch(typeof(Users))
}
};
To make sure, you are looking for a list of User entities that have loaned a particular Book entity. Is this the correct use case, or are you looking for a list of User entities that have borrowed the particular book?
Regardless, LLBLGen's support for these cases is great with referencing relationships between entities and using related entities quickly and easily.
Presuming you're looking up a book by unique BookId/ISBN/etc....
// Get the specific book entity
BookEntity book = new BookEntity(bookId);
foreach(UserEntity user in book.users)
{
// do stuff with list of users
}
It's just that simple assuming you've defined your relationships between entities.