This is a migrated thread and some comments may be shown as answers.

Update in OpenAccess with multiple Domain models

1 Answer 54 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vipin
Top achievements
Rank 1
Vipin asked on 27 Feb 2013, 01:10 PM
I am new to OpenAccess ORM.

Say I have 3 domain models. 
1.Employee
a.EmpID
b.EmpName
c.AddressID
2. Address
a. Id
b. Address Line 1
c. Address Line 2
3.Phones
a. Id
b.AddressID
c.PhoneNumber

Employee and Address have 1 to 1 relation. Where as Address and Phone have 1 to Many relation. I can insert the code to DB by:

Employee.Address = Address;
Employee.Address.Phones.Add(Phone1)
context.Add(Employee)
context.savechanges()


But when Updating, I am having problems. I When I use  code similar to above

Employee emp = Context.Employees.Where(<some condition>).toList().First();

Address.Phones.Add(Phone1)
emp.Address = Address;


context.savechanges()


, no error is thrown, but Phone and Adress is not getting updated.

I feel like I am doing it all wrong. Can you guide me how to do Update with multiple domain levels in OpenAccess ORM?

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 04 Mar 2013, 10:17 AM
Hi Vipin,

Thank your for the detailed explanation.

Generally, updating an object with Telerik OpenAccess ORM is performed by referencing it from the database, changing the values of its properties and calling the SaveChanges method of the context.

In other words, in order to update the phone number of an employee, for example, you need to reference the needed Phone object and update the value of the PhoneNumber property (for details about the update operation check here).

A possible solution that utilizes the multiple levels would look like this:
using (EntitiesModel dbContext = new EntitiesModel())
{
      Employee emp = dbContext.Employees.First(<some condition>);
      Phone phone = emp.Address.Phones.Where(<some conditon>).First();
      phone.PhoneNumber = <a new value for the phone number>;
 
      dbContext.SaveChanges();
}

If you experience any difficulties or have additional questions, do not hesitate to get back to us.

 

Regards,
Doroteya
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Sign up for a free webinar to see all the new stuff in action.
Tags
Databases and Data Types
Asked by
Vipin
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or