One To Many mapping return invalid column name

core ef-fluent-api entity-framework entity-framework-core

Question

I am creating one to many relationship between two entities using ef core using fluent api, but it throws error "Invalid column name 'UploadAttemptStatusId'".

I have tried using data annotation attribute as well as .HasForeignKey method.

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);
        }
1
0
7/16/2019 6:27:19 AM

Popular Answer

I have updated entities like below and it works, but it is not as per FluentAPI convention.

As per FluentAPI convention, we should not have to map foreign key in mapper, it should get mapped directly using .HasOne() or .HasMany() methods. But in my case this was achieved by this line in UploadAttemptsMap

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);
        }

0
7/16/2019 12:09:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow