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

Fluent Mapping Association: cascading update

1 Answer 58 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Roderik
Top achievements
Rank 1
Roderik asked on 08 Nov 2011, 01:49 PM
Dear Telerik, 

I am looking to use OpenAccess in a code first approach so I'm trying to use the fluent mapping API.I have two questions regarding associations. 

1) In the following example, which is based on the documentation example, the CategoryId property seems double. You can get the Category Id by traversing the Category object requesting its id (ex: product.Category.ID). Is this property added for being able to enforce constraints or is it somehow needed by the openaccess infrastructure? If I remove it, the code still works. 

public abstract class Entity : IEntity
{
    public int ID { get; private set; }
}
 
public class Product : Entity
{
    public string Name { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}
 
public class Category : Entity
{
    public Category()
    {
        this.Products = new List<Product>();
    }
 
    public string Name { get; set; }
    public IList<Product> Products { get; set; }
}


2) A one-way optional association. I have a Site entity that has an optional dependency on a Template entity through the property Site.DefaultTemplate. Saving this association to the database works. Now I delete the Template entity and I wish this action also updates the property Site.DefaultTemplate to null. It's associated object is deleted after all. In the documenation I can only find information about Depends() for cascading deletes. I am basically looking for cascade on persist / update. Is there any such thing? 

1 Answer, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 10 Nov 2011, 11:26 PM
Hello Roderik,

 When accessing the CategeoryID property you are retrieving the data from the actual Products table. While you can access it trough .Category.CategoryID this will perform a query on the Categories table. Basically when loading a product you only get a single row's data from the Products table and as soon as you access the Category property another query is fired that retrieves the row for the category from the Categories table. 

While you can do everything using objects like this, you might hit some performance issues later on. 

If you delete the object and then perform a .SaveChanges() operation this should modify the reference property on the other side. Prior to that (calling SaveChanges()), your in-memory model will not be changed. If that is not the case can you please assemble a project that shows the wrong behaviour so that we can locally debug it. (You will need to open a support ticket in order to upload a project)

I hope this is helpful, do let us know if you face further question or have more questions.

Greetings,
Serge
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

Tags
General Discussions
Asked by
Roderik
Top achievements
Rank 1
Answers by
Serge
Telerik team
Share this question
or