Open Closed

Entity Relationship created by ABP Suite #3717


User avatar
0
mat.guthrie@loupa.io created

Hi,

I'm using Entity Framework with SQL Server and am having a few issues with the relationships and the entities generated by abp suite.

For instance, I'd like to create a one-to-many between customer and address where the customer can have multiple addresses. I'd expect to see the Customer entity have a Collection navigation property (as below), but abp suite appears to create a one-to-one relationship with the navigation property defined in CustomerWithNavigationProperties. hHen I review the documentation for AggregateRoot Class it suggests my expected entity structure is correct. What am I doing wrong / missing?

Similarly, I'd like to create a one-to-one relationship between Customer and a second table MarketingStatus. I'd expect to see a Reference navigation property added to Customer entity but I can't seem to achieve this either.


public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Address> Addresses { get; set; }
    public MarketingStatus MarketingStatus { get; set; }
}

Is this possible to achieve using entity generation using abp suite or do I need to defining these by hand. Can you assist?

Thanks

Mat


5 Answer(s)
  • User Avatar
    0
    mat.guthrie@loupa.io created

    Hi,

    NOTE: This is not an answer, just a follow-up question

    Having gone through the free Implementing DDD pdf I see that the reason that navigation properties are not created is so as to remain database provider agnostic. I will only be using EF Core and SQL so this is not an issue for me. I note that the documentation says "If you think such features are important for you and you will never stray from the EF Core, we believe that it is worth stretching this principle."

    Do you have any code samples that make use of entities with navigation properties? Normally I would just use AutoMapper to map the DTO to the entity and sub-entities, however is is not possible because the requirements use Guid as the identity. Is there anyway to get around this or a preferred method that doesn't involve setting every property manually. Something like:

    CreateMap<AddressCreateDto, Address>().ConstructUsing(a => new Address(_guidGenerator.Create()));
    

    An example of the type of entity relationships I'm trying to deal with would be as follows:

        public class Customer
        {
            public Guid Id { get; set; }
            public string Email { get; set; }
            public object FirstName { get; set; }
            public object LastName { get; set; }
            public int CurrencyId { get; set; }
    
            // Navigation Property
            public List<Address> Addresses { get; set; }
    
            // Navigation Property
            public Currency Currency { get; set; }
    
        }
    
        public class Address
        {
            public Guid Id { get; set; }
            public string Line1 { get; set; }
    
            public string City { get; set; }
    
            public string Zip { get; set; }
        }
    
        // Shared Reference Data
        public class Currency
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string IsoCode { get; set; }
        }
    

    Any assistance or sample code would be very helpful.

    Thanks

    Mat

  • User Avatar
    0
    alper created
    Support Team Director

    Ok, Mat I'll try to make an example project via Suite based on your message.

  • User Avatar
    0
    hamzabraim created

    Hello Mat,

    As for your first question, I created a new solution and used ABP Suite to generate a customer entity, and then a address entity. I also added a navigation property (one-to-many) to the address entity and linked it with the customer entity so that each customer can have multiple addresses and it works perfectly as in the screenshots:

    As for the one-to-one relationship, I'm afraid it is not supported.

  • User Avatar
    0
    mat.guthrie@loupa.io created

    Hi,

    When you say "it works perfectly" what do you mean? When I did exactly that, the Customer Entity didn't have a Address collection navigation property and it appeared to create a 1-1 relationship in CustomerWithNaviagtionProperties class. See below. That's not 1 Customer with Many addresses.

    Your screenshots just illustrate the UI for creating Addresses. That's wasn't the question.

    Further assistance would be appreciated. Thanks

    Mat

    public class Customer : FullAuditedAggregateRoot&lt;Guid&gt;, IMultiTenant
    {
        public virtual Guid? TenantId { get; set; }
    
        [CanBeNull]
        public virtual string Name { get; set; }
        public Guid? AddressId { get; set; }
    
        public Customer()
        {
    
        }
    
        public Customer(Guid id, Guid? addressId, string name)
        {
    
            Id = id;
            Check.Length(name, nameof(name), CustomerConsts.NameMaxLength, 0);
            Name = name;
            AddressId = addressId;
        }
    
    }
    
    public class CustomerWithNavigationProperties
    {
        public Customer Customer { get; set; }
    
        public Address Address { get; set; }       
    
        
    }
    
  • User Avatar
    0
    alper created
    Support Team Director

    Hi,

    ABP Suite helps to create basic CRUD pages with limited relationship options. if you need further steps, you should manually edit the generated code.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11