Home > Enterprise >  Many-to-Many Relation in EfCore with DDD
Many-to-Many Relation in EfCore with DDD

Time:07-30

I've just started developing apps with DDD using .Net Core,Ef Core and ABP Framework I have 2 aggregate roots ( Ticket and Asset) in my app.

  1. A Ticket may have an assigned Asset/Assets
  2. An Asset may have assigned Ticket/Tickets
  3. I don't want to hold in the asset root that which tickets that assets assigned(if its possible)

so at this point i am confused how could i implement this?

Since its many-to-many relationship i guess i should create a new Entity in App/Domain/Tickets/TicketAssets.cs that holds ticket ids and asset ids, then navigate to that Entity in Ticket AR as ICollection<TicketAsset>.

Is it correct? what is the best practice in that case.

Ticket.cs

public class Ticket : FullAuditedAggregateRoot<Guid>
    {

        public virtual Guid Id { get; protected set; }
        public virtual TicketSenderType SenderType { get; protected set; }
        public virtual Guid SenderUserId { get; protected set; }

        //Asset Aggregate Root Relation that i thought first but then I decided the way i wrote above         
        public virtual ICollection<Asset> AssignedAssets { get; protected set; }

        public virtual SenderContact SenderInfo { get; protected set; }
        public virtual string Title { get; protected set; }
        public virtual string Description { get; set; }
        public virtual TicketStatus Status { get; protected set; }
        public virtual TicketClosedReason? ClosedReason { get; protected set; }

        //public virtual List<Label> Labels { get; protected set; }
        //public virtual Guid AssignedUserId { get; protected set; }

        private Ticket() { }

        internal Ticket(Guid id, string title, string description, TicketSenderType senderType, SenderContact senderInfo, [CanBeNull] Guid senderId, [CanBeNull] List<Asset> assignedAssetsList) : base(id)
        {
            Id = id;
            Title = title;
            Description = description;
            Status = TicketStatus.Open;
            ClosedReason = null;
            SenderType = senderType;
            SenderUserId = senderId;
            SenderInfo = senderInfo;
            AssignedAssets = new List<Asset>();
        }

Asset.cs

 public class Asset : FullAuditedAggregateRoot<Guid>
    {
        public virtual Guid Id { get; protected set; }
        public virtual Guid AssignedUserId { get; protected set; }
        public virtual Guid ClientId { get; protected set; }
        public virtual string Tag { get;  set; }
        public virtual string Name { get;  set; }
        public virtual string SerialNumber { get; protected set; }
        public virtual AssetStatus Status { get;  set; }
        public virtual string? Detail { get;  set; }
        public virtual bool isAssigned { get; protected set; }
        public virtual DateTime? AssignedAt { get; protected set; }

        private Asset() { }
        internal Asset(
            Guid id,
            string serial,
            string tag,
            string name,
            string detail
            ) : base(id)
        {
            Id = id;
            SerialNumber = serial;
            Tag = Check.NotNullOrEmpty(tag, "Tag");
            Name = Check.NotNullOrEmpty(name, "Name");
            Status = AssetStatus.Waiting;
            Detail = detail;
            AssignedUserId = Guid.Empty;
            ClientId = Guid.Empty;
            isAssigned = false;
            AssignedAt = null;
        }

CodePudding user response:

You don't have to create the join table manually, EF will create that for you.

Since this is many-to-many relationship, both entities must have navigation properties to each other.

// Ticket
public class Ticket : FullAuditedAggregateRoot<Guid>
{
    // ...

    public virtual ICollection<Asset> Assets { get; set; }
}

// Asset
public class Asset: FullAuditedAggregateRoot<Guid>
{
    // ...

    public virtual ICollection<Ticket> Tickets { get; set; }
}

CodePudding user response:

That's the idea, for a detailed example, please read this post: https://community.abp.io/posts/many-to-many-relationship-with-abp-and-ef-core-g7rm2qut

  • Related