Skip to content

عدم پشتیبانی کانفیگ کننده اتوماتیک از کلید اصلی چند گانه #353

Description

@sadrazkh

زمانی که جدولی مانند جداول واسطه از چند کلید اصلی استفاده میکنند از
پیش فرض نمیشه استفاده کرد چرا که بخش سوم صرفا یک کلید را قبول میکند BaseDto
و اجازه نمیدهد ما از کانفیگ خودکار اتومپر استفاده کنیم

public class StudentHomeWorks : IEntity
{
    public int StudentId { get; set; }
    public long HomeWorkId { get; set; }
    public StudentHomeWorkStatus Status { get; set; }
    public Student Student { get; set; }
    public HomeWork HomeWork { get; set; }
}

public class StudentHomeWorksConfiguration : IEntityTypeConfiguration<StudentHomeWorks>
{
    public void Configure(EntityTypeBuilder<StudentHomeWorks> builder)
    {
        builder.HasKey(s => new { s.StudentId, s.HomeWorkId });
        builder.HasIndex(c => c.StudentId);
        builder.HasIndex(c => c.HomeWorkId);
        #region Relations
        builder.HasOne(s => s.Student)
            .WithMany(s => s.HomeWorks)
            .HasForeignKey(s => s.StudentId);
        builder.HasOne(s => s.HomeWork)
            .WithMany(e => e.Students)
            .HasForeignKey(s => s.HomeWorkId);
        #endregion
    }
}

``

public abstract class BaseDto<TDto, TEntity, TKey> : IHaveCustomMapping
    where TDto : class, new()
    where TEntity : class, IEntity<TKey>, new()
{
    [Display(Name = "ردیف")]
    public TKey Id { get; set; }

    public TEntity ToEntity(IMapper mapper)
    {
        return mapper.Map<TEntity>(CastToDerivedClass(mapper, this));
    }

    public TEntity ToEntity(IMapper mapper, TEntity entity)
    {
        return mapper.Map(CastToDerivedClass(mapper, this), entity);
    }

    public static TDto FromEntity(IMapper mapper, TEntity model)
    {
        return mapper.Map<TDto>(model);
    }

    protected TDto CastToDerivedClass(IMapper mapper, BaseDto<TDto, TEntity, TKey> baseInstance)
    {
        return mapper.Map<TDto>(baseInstance);
    }

    public void CreateMappings(Profile profile)
    {
        var mappingExpression = profile.CreateMap<TDto, TEntity>();

        var dtoType = typeof(TDto);
        var entityType = typeof(TEntity);
        //Ignore any property of source (like Post.Author) that dose not contains in destination 
        foreach (var property in entityType.GetProperties())
        {
            if (dtoType.GetProperty(property.Name) == null)
                mappingExpression.ForMember(property.Name, opt => opt.Ignore());
        }

        CustomMappings(mappingExpression.ReverseMap());
    }

    public virtual void CustomMappings(IMappingExpression<TEntity, TDto> mapping)
    {
    }
}

و مساله اینجاست که زمانی که دی تی او رو میسازیم چگونه کانفیگ کینم

public class ViewAllQuestionOfStudentDto : BaseDto<ViewAllQuestionOfStudentDto, StudentHomeWorks, ???>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions