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

时间:2019-07-16 05:44:14

标签: entity-framework entity-framework-core core ef-fluent-api

我正在使用fluent api使用ef核心在两个实体之间创建一对多关系,但是会引发错误“无效的列名'UploadAttemptStatusId'”。

我尝试使用数据注释属性以及.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);
        }

1 个答案:

答案 0 :(得分:0)

我已经更新了如下所示的实体,并且它可以正常工作,但是不是按照FluentAPI约定。

根据FluentAPI约定,我们不必在mapper中映射外键,而应使用.HasOne()或.HasMany()方法直接对其进行映射。 但就我而言,这是通过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);
        }