Base solution for your next web application
Open Closed

Override Default Id as Primary Key #5931


User avatar
0
mdframe created

How do I override the default Id field for a table in ANZ? I have created a master/detail situation with Purchase Orders and the detail table needs to be a composite key.

The scenario:

PurchaseOrder.Id is the the primary key of the parent master table. The child table, PurchaseOrderDetail should be a composite of PurchaseOrderDetail.PurchaseOrderId + PurchaseOrderDetail.LineNbr. I have been unable to determine how best to override the framework to make ANZ allow me to remove the default Id incremental created field and instead use the defined PurchaseOrderDetail.PurchaseOrderId field as the key in this entity/Dto.

Thanks,

Matt


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    This is not recommended. (If you don't inherit Entity, you will lose many of the features of the framework)

  • User Avatar
    0
    mdframe created

    This is a vauge response. What will be lost? How would you recommend maintaining the appropriate parent child relationship as the CreateOrEdit methods all need updating because Id in the child is not the parent. If I was doing database first wouldn't I have the same issue? I thought the framework supported these situations.

  • User Avatar
    1
    ryancyq created
    Support Team

    See https://support.aspnetzero.com/QA/Questions/3232

    Abp built-in repository pattern supprts only Id as primary key. if you want to have composite primary keys, you can do it without going through abp repository interface (however you do lose some advantages from not using abp repository)

  • User Avatar
    0
    mdframe created

    So maybe I'm thinking about this incorrectly. How would I preform a one to many relationship with ANZ with only a single primary key?

  • User Avatar
    0
    ryancyq created
    Support Team

    You can

    1. follow Ef/Ef Core naming convention for entity properties
    2. decorate your entity properties with EF/EF Core Attributes

    e.g. https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.ZeroCore/Authorization/Roles/AbpRole.cs#L39

    see also https://docs.microsoft.com/en-us/ef/core/modeling/relationships

  • User Avatar
    0
    mdframe created

    @ryancyq - thank you for the information and patience. This is my first real experience with EF and code-first and its got a learning curve. I was looking at the Microsoft document today and I was wondering why the RAD tool doesnt add ICollections to the foreign keys but I made the assumption it only knows a relationship exists through the foreign key not to create a collection.

    Should I be adding ICollections to my master/parent tables?

  • User Avatar
    0
    ryancyq created
    Support Team

    @mdframe, yes you can do that. a PurchaseOrder most likely to have a collection of PurchaseOrderLineItem.

  • User Avatar
    0
    mdframe created

    @ryancyq - Thank you for all the information, I really appreciate the help!