How to Show Lookups using Silverlight 4.0 and RIA Services - silverlight

I have a relational database lets say it consist of an Employee's Table and Department's table and an EmployeesType Table.
The employee's table has a DepartmentId, EmployeeTypeId foreign Keys
and now I created a silverlight app with an entity data model and generated a domain service class
and now I want to show employees info in a grid .. of course I can't show Department name and EmployeeType Name in the Grid
I have to use the Include Data annotation in my metadata ..
I did this .. but how can I show all the included fields in one query?
I used this
public IQueryable<Employee> GetEmployeesWithDepartments()
{
return this.ObjectContext.Employees.Include("Department.EmployeesType");
}
but I managed to show departments only.. What About the other foreign keys?
How can I add them to my query?

When you bind to Employee - you access Department id something like:
{Binding Employee.Department.Id}
And you access EmployeeType like
{Binding Employee.EmployeesType.Id}
Is that what you asking?

Ok guys .. so after two freaking days of trial and error .. i finally found it
here is what you should do ..
in your getEmployees Method or whatever method of retrival you use you can include all your forign keys as follow
public IQueryable<Employee> GetEmployeesWithDepartments()
{
return this.ObjectContext.Employees.Include("Department").Include("EmployeeType");
}
and so on for every forgin key you have included in your metadata class
then in the front end you can now have access to the Department Property and use the Binding syntax to bind it with your View
as follow:
<TextBlock Text="{Binding Department.DepartmentName"/>
Thanks for trying to help

Related

Is class diagram required to change if DB contains Views on tables?

I'm a newbie in designing class diagrams. I follow DAO pattern in my design.
In my project, one user can add multiple contacts which can further be added to many groups.
Let's say 1 contact -> many groups.
As per my requirement UserContact and UserGroup are classes which manage contacts and groups.
In DB contacts and groups are stored in two different tables.
There is one use case to retrieve all contacts along with their groups so that I need to retrieve all contacts first and then using contactID again I need to make a query to get its relative groups.To avoid this in DB, there is VIEW on these two tables.
Now,with VIEW, I need to make one query to get user contacts and groups.
How can I add this method in DAO?
How do I change my classes so that contacts and groups can be mapped to my objects?
The following are the classes involved in this use case.
If I understand the question correctly; you can create a GroupContactDAOImpl class that has only query (get) methods, e.g. getGroupContacts(groupId) or getAllGroupContacts() if you will. Since "group contacts" is not an entity that has its own table, UserContactDAOImpl.addContact and UserGroupDAOImpl.addGroup methods would be sufficient to add new records.
If I need to get groups associated with one contact, how can I map contact information and groups to one object?
If you want to map contact info and groups to one object, you can add a UserGroup property to UserContact class (like in Hibernate). Then GroupContactDAOImpl will have a method like: UserContact getUserContactWithGroups(contactID).
Similarly, if you'd like to get a group's info with all the contacts asscoiated; you can add a UserContact property to UserGroup class. GroupContactDAOImpl will have a method like: UserGroup getUserGroupWithContacts(groupID).
There might be other methods to do the same thing; hope this helps.
As I need to insert and update a contact using UserContact, it should contain only contactID, email and name. I use UserContact and UserGroup as DTOs. Can I create a new class which holds UserContact and UserGroup that uses UserContactDAOImpl and is it a preferable way?
Yes, you can follow that way if you'd like to leave UserContact class as it is. You'd have a new DTO (e.g UserContactWithGroups) and return it from UserContactDAOImpl (e.g .getContactWithGroups method.) You can also create a separate DAO class, but it doesn't matter much. It's best to adapt it to your own use case.

Many-to-Many relationship in Zend 2 Framework

I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'

How to load only few properties from entity?

I am using EF 4.0 and I have created 3 entities from DB.
In DB, I have the following tables Employee,Contact,Department.
Employee Table -> EmployeeID,Name,Designation
ContactTable -> ContactID,Address,CellNo,EMployeeID(FK)
Department Table -> DepartmentID,Name,EmployeeID(FK)
After converting into entities from DB, I get following entities
EmployeeObject ->
EmployeeID,Name,Designation,Contact(Another Entity),Department(Another Entity).
Till this its ok, now I want to bind the employees to data grid its also work fine...
I use below code to bind employees to record ...
code working perfectly, now issue is when I see the data into grid , Two extra column comes
into grid, Contacts and Departments.
Please see the image
Click here to see screenshot of result grid
Entity Model Pic
I want to bind only records which are into employee table not the object which are in
entity (Contacts and Department.)

Displaying fields from related tables in a Silverlight datagrid

I have a Silverlight 4.0 application that has a normalised database. In this database i have tables for Applicants, Licences, LicenceClasses, LicenceTypes and LicenceStatuses amongst others. The last 3 mentioned tables are lookup tables linked to the Licences table through foreign key relationships.I am using RIA services with the Entity Framework for data access. The scenario i am facing is as follows.
When i create a datagrid on my form i get all the appropriate colums with fields from the Licences table. I want to display the names from the lookup tables that are represented by the ID fields in the Licence table. I need to show for instance the LicenceStatus instead of the LicenceStatusID.
I have followed some examples about including the related collections in my domain service and making all the appropriate Include annotations in the Metadata classes. While i can correctly get this to work with one lookup field , i can't seem to find a way to include more than one look up table in my GetLicences query.
public IQueryable<LearnersLicence> GetLearnersLicences()
{
return this.ObjectContext.LearnersLicences.Include("LicenceClass");
}
In the above query i can only include the LicenceClass collection and i have found no way of including the LicenceStatus collection or multiple look up collections that i need to display.
How do i go about accomplishing this
You can include multiple tables by adding a include for each.
public IQueryable<LearnersLicence> GetLearnersLicences()
{
return this.ObjectContext.LearnersLicences.Include("LicenceClass").Include("LicenceTypes");
}

Dynamic fields on insert/edit form

let's say that i have 3 tables:
books
properties
book_properties
of course, i would like that when i want to insert new book (or update existing), to fill the form.
fields that exist on form have to be defined as records in table properties.
when i fill up those fields, data has to be saved in table book_properties.
can you help me by giving some advices and references, how to achieve that?
thank you very much in advance!
You will need to create 3 tables: books (id, author, title, etc.), properties (id, name) and books_properties (id, book_id, property_id, property_value). Notice that the third table must be named books_properties, not book_property. Then you will need to create models for the books and properties tables. See the manual here. Create correct assosiations. Books HasAndBelongsToMany Properties. If you use cake console it will be easier to create models, controllers and views.

Resources