Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Invalid column name 'EmploymentTypeEntityEmploymentTypeID in Entity framework core

I am getting the below error.

Message: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details. ----> System.Data.SqlClient.SqlException : Invalid column name 'EmploymentTypeEntityEmploymentTypeID'.

Its strange as its combining my Entity Class Name and the Entity Property Name.

Below is my code.

SystemTest.cs

   using (var transaction = _referenceDataDbContext.Database.BeginTransaction())
            {
                _referenceDataDbContext.EmploymentType.AddRangeAsync(

                    new EmploymentTypeEntity
                    {
                        EmploymentTypeID = 1,
                        EmploymentType = "EmploymentType",
                        CategoryTypeID = 27,

                        SiteAddress = null,
                        CreatedBy = "UnitTest",
                        CreatedOn = DateTime.Now,
                        ModifiedBy = "UnitTest",
                        ModifiedOn = DateTime.Now,
                        RowVersion = new RowVersion(1),
                        EmploymentTypeGroups = new[]
                        {
                        new EmploymentTypeGroupEntity
                        {
                            EmploymentTypeGroupID = 11, GroupName = "Child Care", IsActive = true
                        }
                        }
                    }

                    }
                );

                _referenceDataDbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [ref].[EmploymentType] ON");

                _referenceDataDbContext.SaveChanges();
            }

EmploymentTypeGroup.cs

  public class EmploymentTypeGroupEntity
    {
        [Key]
        public int? EmploymentTypeGroupID { get; set; }
        public string GroupName { get; set; }
        public bool? IsActive { get; set; }
        public DateTime? CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? ModifiedOn { get; set; }
        public string ModifiedBy { get; set; }

        [Timestamp]
        [ConcurrencyCheck]
        public byte[] RowVersion { get; set; }

    }

EmploymentType.cs

public class EmploymentTypeEntity
    {
        [Key]
        public int? EmploymentTypeID { get; set; }
        public string EmploymentType { get; set; }
        public int? CategoryTypeID { get; set; }

        public bool? SiteAddress { get; set; }
        public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }
        public DateTime? CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? ModifiedOn { get; set; }
        public string ModifiedBy { get; set; }

        [Timestamp]
        [ConcurrencyCheck]
        public byte[] RowVersion { get; set; }
    }

DataDbContext.cs

public class ReferenceDataDbContext : DbContext
    {
        public ReferenceDataDbContext(DbContextOptions<ReferenceDataDbContext> options)
            : base(options)
        {
        }

        public ReferenceDataDbContext()
        {

        }


        public virtual DbSet<EmploymentTypeEntity> EmploymentType { get; set; }
        public virtual DbSet<EmploymentTypeGroupEntity> EmploymentTypeGroup { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<StateEntity>().ToTable("State", "ref");
            builder.Entity<EmploymentTypeGroupEntity>().ToTable("EmploymentTypeGroup", "ref");
            builder.Entity<EmploymentTypeEntity>().ToTable("EmploymentType","ref").HasMany(a => a.EmploymentTypeGroups);

            // Configure database attributes
        }
    }
like image 548
maxspan Avatar asked Sep 26 '18 22:09

maxspan


1 Answers

You are creating a relationship between EmploymentTypeGroupEntity and EmploymentTypeEntity. But you are not telling Entity Framework what that relationship is. EF has guessed that you want a reference to EmploymentTypeEntity in your EmploymentTypeGroupEntity table and created a field for that. This clearly isn't what you want.

You need to tell EF what the relationship is. If it is a one-many relationship where one EmploymentTypeEntity can have many EmploymentTypeGroupEntity's, which appears to be the case because you have defined:

public IEnumerable<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }

You also need to create a foreign key in your EmploymentTypeGroupEntity class. so add to this class:

public int EmploymentTypeEntityID { get; set; }

[ForeignKey(nameof(EmploymentTypeEntityID))]
public EmploymentTypeEntity EmploymentTypeEntity  { get; set; }

In your EmploymentTypeEntity class change the collection type:

public ICollection<EmploymentTypeGroupEntity> EmploymentTypeGroups { get; set; }

Add a constructor to assign a new List<EmploymentTypeGroupEntity>() to EmploymentTypeGroups.

Change the array assignment in your tests to add to the collection and add the foreign key to the groups creation.

like image 81
Philip Smith Avatar answered Oct 16 '22 19:10

Philip Smith