New Posts New Posts RSS Feed: Refresh Related Entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Refresh Related Entity

 Post Reply Post Reply
Author
ngoel View Drop Down
Newbie
Newbie


Joined: 17-Nov-2009
Posts: 15
Post Options Post Options   Quote ngoel Quote  Post ReplyReply Direct Link To This Post Topic: Refresh Related Entity
    Posted: 28-Feb-2010 at 8:01pm

We have a Employee and Department Many to One relationship where an employee is attached to single deparment. Employee table has DepartmentId as a Foriegn Key attached to the DepartmentId column in the Department table. When editing the employees, user change the DepartmentId and it automatically update the Department object attached to the Employee entity which is what I want.

But, when creating a new employee, I create a Employee with a Blank Department first and then update the DepartmentId on the Employee object according to the user selection. But this does not update the Department object attached. The Department Object attached is still a Null Entity. Is there a way to force an update on the Employee table so that it updates the department object according to the department id selected? I think we might have to detach the Department object and reattach it. But not sure how I do it. Can you help?

Back to Top
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Posted: 04-Mar-2010 at 2:14pm
I don't quite get this. Presumably you aren't assigning the null department entity to your Employee; so I don't understand how the Department property of your Employee ends up being the null entity.

I suspect that maybe there are some UI refresh issues involved in what you think you're seeing, though I can't quite figure out the scenario. But consider the following console app code:

    private void DoIt() {
      Customer aCustomer = _mgr.Customers.FirstOrNullEntity();
      Order newOrder = Order.Create(_mgr);
      Console.WriteLine("New Order's Customer: {0}", newOrder.Customer.CompanyName);
      newOrder.PropertyChanged += new syscomp.PropertyChangedEventHandler(newOrder_PropertyChanged);
      newOrder.Customer = aCustomer;
      Console.WriteLine("New Order's Customer after assignment: {0}", newOrder.Customer.CompanyName);
      PromptToContinue();
    }

    void newOrder_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
      Console.WriteLine("Property changed on Order: {0}", e.PropertyName );
    }

It produces the following output:




Back to Top
ngoel View Drop Down
Newbie
Newbie


Joined: 17-Nov-2009
Posts: 15
Post Options Post Options   Quote ngoel Quote  Post ReplyReply Direct Link To This Post Posted: 08-Mar-2010 at 7:09pm
Hello
 
I am not assigning the null department entity to your Employee. But I think when you create an Employee object with just the Primary Key values, it puts null entity for the Department. For setting the Department value, I am doing it a bit differently. Here is my code:
 
Employee emp = Employee.Create(_mgr);
Department department = GetSelectedDepartment();
 
Now, instead of doing emp.Department = department;
 
I am doing, emp.DepartmentId = department.DepartmentId;
 
This way, the DepartmentId gets updated, but emp.Department.DepartmentId is still blank, which suggests that the Department object had not been updated. I think I have to manually update the department object.
 
Thanks
Nitin
 
 
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 09-Mar-2010 at 6:51pm
Originally posted by ngoel

But I think when you create an Employee object with just the Primary Key values, it puts null entity for the Department.
 
No, not really.  When you first create the Employee object, the foreign key to Department is null. DevForce doesn't change that on its own. 
 
However, if DevForce is asked to look up a Department with a foreign key of null (for example, when you reference newEmployee.Department), naturally it can't find one. DevForce could just let an exception be thrown: that's the standard .NET behavior when you refer to something that doesn't exist. But it likes to help you out; so instead of throwing an exception, it return a null entity for a Department. That way, your code doesn't blow up; but you (and your user) can still tell that there's no Department yet assigned.  The foreign key remains null.
 
 
Originally posted by ngoel

I am doing, emp.DepartmentId = department.DepartmentId;

It does take some time to get used to not having to do all of the wiring via the foreign keys.

Back to Top
ngoel View Drop Down
Newbie
Newbie


Joined: 17-Nov-2009
Posts: 15
Post Options Post Options   Quote ngoel Quote  Post ReplyReply Direct Link To This Post Posted: 15-Mar-2010 at 3:17pm
 
I undertstands that it returns a Null Entity for Department if there isn't one available. My problem is when I am setting up the Department Id in the Employee table (Department Id is a Foriegn Key relating the Department table to the Employee Table), it is not updating the Null Entity to Actual Department. My question was is there a way to force an update. The way I am setting the Department Id is
 
emp.DepartmentId = department.DepartmentId; ( According to the User Selection)
 
It works if we already have a different department attached i.e. user is just changing the department, but it does not work when the Department attached to the Employee is a null entity. I hope I made myself clear this time.
 
Thanks
Nitin
Back to Top
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Posted: 16-Mar-2010 at 2:01pm
Sorry, but there is much I don't understand about your post. First of all, you refer to "setting up the Department Id in the Employee table," which sounds like some operation you're performing directly on a database table rather than in an object model. In the object model, there are no tables! 
 
Secondly, if you actually defined a relationship between the Employee and Department tables in the database, using Employee.DepartmentId as the foreign key to relate the two, then your object model would have been generated automatically with an association between the Employee and Department entities, and the Employee entity would not even have a DepartmentId property: it would only have a Department property. So I'm really not at all clear on what you're doing.  I'm concerned that you may be misunderstanding the object model in some fundamental way, but I just can't yet tell exactly what it is that you're doing.
 
So let's start with this question:  Have you, in fact, defined a relationship in the database between Employee and Department, using Employee.DepartmentId as the foreign key that links to Department.DepartmentId?  If not, why not?
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down