Base solution for your next web application
Open Closed

Many-To-Many relationship with Entity Framework #3658


User avatar
0
Ricavir created

Hello support,

I have following issue :

I have added a collection to User entity like this :

/// <summary>
    /// Represents a user in the system.
    /// </summary>
    public class User : AbpUser<User>
    {
        public const int MinPlainPasswordLength = 6;

        public const int MaxPhoneNumberLength = 24;

        public virtual Guid? ProfilePictureId { get; set; }

        public virtual bool ShouldChangePasswordOnNextLogin { get; set; }

       <span style="color:#FFFF00">public virtual ICollection<Event> Events { get; set; } </span>

As you can see, a collection of Event entities is added.

I also added a collection of User entity in Event entity like this :

[Table("LsEvents")]
    public class Event : FullAuditedEntity<long>, IMustHaveTenant
    {
        public virtual int TenantId { get; set; }

        [ForeignKey("AddressId")]
        public virtual Address Address { get; set; }
        public virtual long AddressId { get; set; }

        [Required]
        public virtual DateTime Begin { get; set; }

        [Required]
        public virtual DateTime End { get; set; }

        public virtual string Description { get; set; }

        public virtual ICollection<User> EventUsers { get; set; }

I've run the migrations on the database and everything went as excepted : foreign keys added to both table AbpUsers and LsEvents and an additional table UserEvents has been added automaticaly to manage many-to-many relationship.

Now, looking to the Event application service : I've created my DTO's to get, create and update Events. I am able to add a new event object with some Abp users as collection. No problem with this.

The problem is to update existing Event entities... When my Event DTO is received, the mapping is working well but User properties are being validated and following error is trhrown :

ERROR 2017-08-04 15:03:12,810 [8 ] EntityFramework.DbContext - There are some validation errors while saving changes in EntityFramework: ERROR 2017-08-04 15:03:12,810 [8 ] EntityFramework.DbContext - - Password: Le champ Password est requis.

Actually, I just want to update the link between Event and Users ... and not update each User entity !

Can you please provide a solution for that ?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    duplicate of #3657