一对多映射返回无效的列名



>我正在使用具有流畅 API 的 ef core 在两个实体之间创建一对多关系,但它抛出了错误

列名称"上传尝试状态 ID"无效。

我尝试使用数据注释属性以及.HasForeignKey方法。

public class UploadAttempts : BaseEntity
{
    public Guid UploadedAttemptId { get; set; }
       
    public string ClientFileName { get; set; }
    public DateTime CreationDateTimeUtc { get; set; }
    public Guid UploadingUserId { get; set; }
    public int UploadAttemptStatusId { get; set; }
    public virtual UploadAttemptStatusDictionary UploadAttemptStatusDictionary { get; set; }
}
public class UploadAttemptStatusDictionary : BaseEntity
{
    public int UploadAttemptStatusId { get; set; }
    public string Description { get; set; }
    public virtual ICollection<UploadAttempts> UploadAttempts { get; set; }
}

public void Map(ModelBuilder modelBuilder)
{
    var entity = modelBuilder.Entity<UploadAttemptStatusDictionary>();
    //set table name
    entity.ToTable("Upload_attempt_status_dictionary");
    entity.Property(t => t.UploadAttemptStatusId).HasColumnName("Upload_attempt_status");
    entity.HasKey(t => t.UploadAttemptStatusId);
    entity.Property(t => t.Description).HasColumnName("description").IsRequired();
    entity.HasMany(t => t.UploadAttempts).WithOne(t=>t.UploadAttemptStatusDictionary);
}

public void Map(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<UploadAttempts>();
            //set table name
            entity.ToTable("Upload_attempts");
            entity.Property(t => t.UploadedAttemptId).HasColumnName("upload_attempt_id");
            entity.HasKey(t => t.UploadedAttemptId);
            
            entity.Property(t => t.ClientFileName).HasColumnName("client_filename");
            entity.Property(t => t.UploadingUserId).HasColumnName("uploading_user_id");
            entity.Property(t => t.CreationDateTimeUtc).HasColumnName("creation_datetime_utc");
            entity.HasOne<UploadAttemptStatusDictionary>(t => t.UploadAttemptStatusDictionary)
                .WithMany(t => t.UploadAttempts)
                .HasForeignKey(t => t.UploadAttemptStatusId);
        }

我已经更新了如下所示的实体并且它可以工作,但它不符合 FluentAPI 约定。

根据 FluentAPI 约定,我们不必在映射器中映射外键,它应该直接使用 .HasOne(( 或 .HasMany(( 方法。但就我而言,这是通过上传尝试地图中的这一行实现的

entity.Property(t => t.UploadAttemptStatus).HasColumnName("upload_attempt_status");
public class UploadAttempts : BaseEntity
    {
        public Guid UploadedAttemptId { get; set; }
        public string ClientFileName { get; set; }
        public DateTime CreationDateTimeUtc { get; set; }
        public Guid UploadingUserId { get; set; }
        public int UploadAttemptStatus { get; set; }
        public virtual UploadAttemptStatusDictionary UploadAttemptStatusDictionary { get; set; }
    }
public class UploadAttemptStatusDictionary : BaseEntity
    {
        public int UploadAttemptStatus { get; set; }
        public string Description { get; set; }
        public virtual ICollection<UploadAttempts> UploadAttempts { get; set; }
    }
public void Map(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<UploadAttempts>();
            //set table name
            entity.ToTable("Upload_attempts");
            entity.Property(t => t.UploadedAttemptId).HasColumnName("upload_attempt_id");
            entity.HasKey(t => t.UploadedAttemptId);
            entity.Property(t => t.UploadAttemptStatus).HasColumnName("upload_attempt_status");
            entity.Property(t => t.ClientFileName).HasColumnName("client_filename");
            entity.Property(t => t.UploadingUserId).HasColumnName("uploading_user_id");
            entity.Property(t => t.CreationDateTimeUtc).HasColumnName("creation_datetime_utc");
            entity.HasOne<UploadAttemptStatusDictionary>(t => t.UploadAttemptStatusDictionary)
                .WithMany(t => t.UploadAttempts)
                .HasForeignKey(t => t.UploadAttemptStatus);
        }
public void Map(ModelBuilder modelBuilder)
        {
            var entity = modelBuilder.Entity<UploadAttemptStatusDictionary>();
            //set table name
            entity.ToTable("Upload_attempt_status_dictionary");
            entity.Property(t => t.UploadAttemptStatus).HasColumnName("upload_attempt_status");
            entity.HasKey(t => t.UploadAttemptStatus);
            entity.Property(t => t.Description).HasColumnName("description").IsRequired();
            entity.HasMany(x => x.UploadAttempts);
        }

相关内容

  • 没有找到相关文章

最新更新