How to auto-load details (with conditions) associated with an entity using Ria Services? - silverlight-4.0

I'm developing a project using Silverlight 4 and Entity Framework 4 and I'm trying to auto-load the details (with conditions) associated with an entity when the client loads the EntityQuery.
So far, I've been able to put in place a solution, using the Include attribute, that returns all the details associated with the master entity. What I'm missing here is to be able to filter out the details based on some criteria.
As an example, here's what my entities look like:
Entity Movie
Id (int)
[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)
Entity MovieLocalizedInformation
Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title
On my DomainService object, I expose the following method:
public IQueryable<Movie> GetMovies( string languageCode )
{
return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}
This works fine. But when I try to add where clause to filter out the localized information based on the language code, only the movies get loaded on the client.
Is there a way to achieve the filtering in one query?
Note: I'm also using the DomainDataSource with paging on the client so the solution needs to work with that.
Any help would be greatly appreciated!
Thanks,
Jacques.

Not sure about Enitity Framework but with a LinqToSqlDomainService you use the LoadWith loadOption
to include the details entities and then use the AssociateWith LoadOption to filter the detail e.g
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Movies>(i => i.MovieLocalizedInformations);
options.AssociateWith<Movies>(i => i.MovieLocalizedInformations.Where(d=> myListOfIds.Contains(d.LocationId)));

Ok,
For efficiency reason, I decided to go with custom DTO object that fetches the localized information and flatten the result.
But, the same problem occurred when my custom DTO needed to reference another custom localized DTO.
Here is how I came to do the same as the .Include( "PropertyName" ) that the ObjectSet offers:
Entity LocalizedMovieCollection
public class LocalizedMovieCollection
{
[Key]
public int Id { get; set; }
public string Name { get; set; } (the result of a sub query based on the language)
[Include]
[Association( "LocalizedMovieCollection_LocalizedMovies", "Id", "MovieCollection_Id" )]
public IEnumerable<LocalizedMovie> Movies { get; set; }
}
Entity LocalizedMovie
public class LocalizedMovie
{
[Key]
public int Id { get; set; }
public string Name { get; set; } (the result of a sub query based on the language)
public int MovieCollection_Id { get; set; }
[Include]
[Association( "LocalizedMovie_LocalizedMovieCollection", "MovieCollection_Id", "Id", IsForeignKey = true]
public LocalizedMovieCollection MovieCollection { get; set; }
}
Then, I've declared two methods: One that returns an IQueryable of LocalizedMovieCollection and the other, an IQueryable of LocalizedMovie. (Note: There must be at least one method that returns each type of entity for the entity to get auto-generated on the Silverlight client)
My goal is to automatically load the MovieCollection associated with a Movie so the method definition to get the movies is as follow:
public IQueryable<LocalizedMovie> GetMovies( string languageCode )
{
return from movie in this.ObjectContext.Movies
join movieLocalizedInfo in this.ObjectContext.MovieLocalizedInformations
on movie equals movieLocalizedInfo.Movie
join movieCollection in this.ObjectContext.MovieCollections
on movie.MovieCollection equals movieCollection
join movieCollectionLocalizedInfo in this.ObjectContext.MovieCollectionLocalizedInformations
on movieCollection equals movieCollectionLocalizedInfo.MovieCollection
where movieLocalizedInfo.LanguageCode == languageCode && movieCollectionLocalizedInfo.LanguageCode == languageCode
select new LocalizedMovie()
{
Id = movie.Id,
Name = movieLocalizedInfo.Name
MovieCollection_Id = movieCollection.Id,
MovieCollection = new LocalizedMovieCollection(){ Id = movieCollection.Id, Name = movieCollectionLocalizedInfo.Name }
}
}
When the Silverlight client loads the query, all the LocalizedMovies and their associated LocalizedMovieCollections will be loaded into the context.

Related

Entity Framework Core - manually assigning id's to linked entities

I've reviewed other related questions on StackOverflow, but I haven't found one that matches my exact question or scenario.
I have an JavaScript front-end where I have two entities that are linked, so in the front-end I have JSON objects that get 'linked' to each other like this:
entity1
name = "something"
entity2Id = 0
entity2
1st record
id = 0
name = "something else"
2nd record
id = 1
name = "something else again"
I then send the JSON to a .NET back end, and the idea is that as I'm manually specifying the primary key for entity2 and setting that as the 'foreign key' reference in my entity1, Entity Framework then somehow needs to generate a valid id for entity2, and maintain a reference to that id in the entity2id property of entity1.
I suspect I may be getting this all horribly wrong and that there's a fairly standard approach to achieving what I'm trying to do. What should I do to achieve the desired result? (I'm using a SQLite database, which probably doesn't make a difference.)
You have a one-to-many relationship there. To make it easier to explain I'll change your Entit1 to Book and Entity2 to Genre. So something like this:
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public int GenreId { get; set; }
public Genre Genre { get; set; }
}
public class Genre
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Book> Books { get; set; }
}
You can then use the FluentApi to configure the relationship (most importantly the FK key on Books from Genres
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>()
.HasOne(p => p.Genre)
.WithMany(b => b.Books)
.HasForeignKey(p => p.GenreId);
}
Now we can achieve your two scenarios:
1 - Create a new Book along with a new Genre
2 - Create a new Book that uses an existing Genre on db
var bookWithNewGenre = new Book
{
Name = "Book 1",
// Here we are creating a new Genre, without Id.
// GenreId here will have the default value of 0,
// which EF will use to find out that it has to be created
Genre = new Genre { Name = "Mistery"}
};
var bookWithExistingFictionGenre = new Book
{
Name = "Book 2",
// Here we are specifying the GenreId = 1 which already exists in the Database
GenreId = 1,
// You don't need to set this to null
// but I like doing it to make EF and my code clear that I'm using an existing Genre
Genre = null
};
using (var context = new BookContext())
{
context.Books.Add(bookWithNewGenre);
context.Books.Add(bookWithExistingFictionGenre);
await context.SaveChangesAsync();
}
After saving you'll have this in the database:
You'll probably have to change your front-end to start sending also the Genre object along. In cases where it's a new one, the Id will be missing. When you serialize it into your c# types, you can figure it out if you have to create new instance or just maybe check if the Id front-end passed exists or not.
Remarks: I did all this using Sqlite and the EF core packages version 2.2.0-preview1-35029.

NHibernate JoinAlias on collection multiple times

I'm using NHibernate 3.33 and QueryOver with Postgre 9.2.
I've got two entities:
public class User {
public virtual string Name { get; set; }
public virtual IList<Reports> Reports { get; set; }
}
and
public class Report {
public virtual string Type { get; set; }
public virtual DateTime ReportDate { get; set; }
public virtual User Author { get; set; }
}
with association - one-to-many (I didn't append additional fields to entities like Id or Name to snippets above). Some report's types are avaliable - month, day.
My goal is to get summary for user - find out whether user has day-report and month-report for current day.
Note: month-report's ReportDate looks like first day of month. Also I want to get it as one row (if it was an SQL) to transform to dto:
public class UserSummaryDto {
public bool HasDayReport { get; set; }
public bool HasMonthReport { get; set; }
}
To achieve my goal I've tried following:
Report dayReport = null;
Report monthReport = null;
var currentDay; // some value of current day
var firstDay; // some value of first day of month
var report = session.QueryOver<User>
.Left.JoinAlias(u => u.Reports, () => dayReport, r => r.ReportDate == currentDay)
.Left.JoinAlias(u => u.Reports, () => monthReport, r => r.ReportDate == firstDat)
.SelectList(
// some logic to check whether user has reports
.TransformUsing(Transformers.AliasToBean<UserSummaryDto>())
.List<UserSummaryDto>()
And I've got error:
'duplicate association path:Reports'.
Is it possible to avoid this problem or it's a limitation of HNibernate?
To answer your question:
...Is it possible to avoid this problem or it's a limitation of HNibernate?
Have to say NO.
For more information see similar Q & A: Rename NHibernate criteria
We are not querying the DB, not using SQL (which does allow to do a lot). Here we work with "mapped" domain model, and that could bring some limitations - as the one discussed here...
If that could help, the workaround is to map such property twice and use the WHERE clause: 6.2. Mapping a Collection
where="" (optional) specify an arbitrary SQL WHERE condition to be used when retrieving or removing the collection (useful if the collection should contain only a subset of the available data)

Empty assosiated entities in EF 6.0 Fluent Api

I try to implement 1..0 relations with Entity Framework 6. I use instance associsaltion. I try to repeat the examples from web and forums but somehow it doesn't work for me. Please, help.
Entities:
public class CustomerWithFk : Item // Item contains Id
{
public string Name { get; protected set; }
public virtual City City { get; set; } // relation property. Can be 1 or 0
public virtual Product Product { get; set; }
public decimal Money { get; protected set; }
}
public class City : Item
{
public string Name { get; protected set; }
}
Mappings:
public CityMap()
{
ToTable("Cities");
HasKey(c => c.Id);
}
public CustomerFkAssosiationMap()
{
ToTable("Customers");
HasKey(c => c.Id);
HasRequired(g => g.City)
.WithRequiredDependent();
HasRequired(g => g.Product)
.WithRequiredDependent()
.Map(x => x.MapKey("ProductId"));
}
Database tables:
SQL Profiler gives me enxt SQL request:
SELECT
1 AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[Money] AS [Money],
[Extent1].[CityId] AS [CityId],
[Extent1].[ProductId] AS [ProductId]
FROM [dbo].[Customers] AS [Extent1]
So, I don't see any joins here to load data from Cities or Products.
And the result is Null:
I tried different mapping options, like: HasOptional, WithRequiredPrincipal, tried to add Customers proeprty to City (while it's incorrect and City doesn't have to know something about customers)
Nothing helps. The assosiated entities are always null.
Where am I wrong?
The problem is that you are not including the related objects. Try something like this using Include:
var list = context.CustomerWithFk
.Include("City")
.Include("Product");
That tells Entity Framework that you want to pull back the customer along with the city and product. Here is some further reading if you are interested: http://msdn.microsoft.com/en-us/data/jj574232.aspx.
EDIT: You could also enable lazy loading (based on your comment I believe it is what you are after) by adding this to your context:
context.ContextOptions.LazyLoadingEnabled = true;
Read more about lazy loading here: http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx.

Initializing referenced objects in entity framework unit of work

I have a class in Entity framework 5 (using MVC 4):
public class JobFunction
{
public int Id { get; set; }
public string JobFunctionName { get; set; }
public int StatusId { get; set; }
public Status JFStatus { get; set; }
}
In my OnModelCreating method, I establish a FK relationship with the Status table as follows:
modelBuilder.Entity<JobFunction>().HasRequired(a => a.JFStatus).
WithMany().HasForeignKey(u => u.StatusId).WillCascadeOnDelete(false);
In my controller, I get a list of JobFunction objects as follows:
List<JobFunction> jfList = uow.JobFunctionRepository.GetAll().ToList<Catalog>();
where uow is my Unit of Work object, and JobFunctionRepository is defined. When I examine any JobFunction object in jfList, I see the following in my watch window:
Id: 1
JfStatus: null
JobFunctionName: "Manager"
StatusId: 2
Note that JFStatus is null. My question is: what provisions do I make in my code to initialize JFStatus to the appropriate Status object (based on the value of StatusId), during my GetAll call?
Thanks in advance.
-Zawar
You need some instrument to apply eager loading when you load the data through your repository. For example you could give your GetAll method a parameter list of expressions for the navigation properties you want to include in your query:
using System.Data.Entity;
//...
public IQueryable<JobFunction> GetAll(
params Expression<Func<JobFunction, object>>[] includes)
{
IQueryable<JobFunction> query = context.JobFunctions;
foreach (var include in includes)
query = query.Include(include);
return query;
}
Then you call it like so:
List<JobFunction> jfList = uow.JobFunctionRepository
.GetAll(jf => jf.JFStatus)
.ToList();
The JFStatus property should be filled now.

Association properties on Entity not loaded for server-side validation

Consider the following situation. I have an Entity named ProductSupplier that is a Presentation Model. It is created by doing an inner join of Products and Suppliers, and creating a new projection from a Linq statement. The ProductSupplier projection also creates a list of PartType objects, which is also a Presentation Model.
public partial class ProductSupplier
{
private IEnumerable<PartType> _partTypes;
[Key]
public int ProductSupplierKey { get; set }
[Include]
[Association("ProductSupplier_PartType", "ProductSupplierKey", "ProductSupplierKey")]
public IEnumerable<PartType> PartTypes
{
get { return _partTypes ?? (_partTypes = new List<PartType>()); }
set { if (value != null) _partTypes = value; }
}
}
public partial class PartType
{
[Key]
public int PartTypeKey { get; set; }
[Key]
public int ProductSupplierKey { get; set; }
public int PartQuantity { get; set; }
}
I want to have a validation that is no ProductSupplier can be have more than 10 separate parts. This means that all PartQuantities for all PartTypes that belong to a ProductSupplier should be summed, and the total cannot exceed 10.
For this, I created a custom validator:
public static ValidationResult ValidatePartTotals(ProductSupplier productSupplier, ValidationContext validationContext)
{
if (productSupplier.PartTypes.Sum(p => p.PartQuantity) > 10)
return new ValidationResult("Must be less than 10 parts total.");
return ValidationResult.Success;
}
This works fine when validation is called from the client-side. The problem I'm having is that when the validator is run from the server-side, the IEnumerable is always empty.
I have tried adding [RoundTripOriginal] to the PartQuantity, and to various other properties, such as all the Key fields, but it still is an empty list when done on the server side.
How can I access these PartType objects when validation is run on the server?
Unfortunately, you don't have any guarantees as to the state of the object graph when it gets to you on the server. RIA optimizes things so you'll only see modified entities. One solution would be to use composition. It will make sure the whole graph is passed around, but it has other effects that may not be what you want. Another option would be to hydrate the graph in your update method, then perform validation, and throw a ValidationException as necessary.