Meghana M

Meghana M

  • 1.6k
  • 175
  • 23.2k

insert data from source to destination with no primary key

Mar 25 2023 10:41 AM

 how to Insert data from source to destination with no primary key  & column names are invalid 

Data.cs(source class ) no primary and foreign key relationship and column are same datatype but names are different between source and destination table

 public class Data
    {
        public string Abc { get; set; }
        public string cde { get; set; }
        public string efg{ get; set; }
      }

Data2.cs(destination class)no primary and foreign key relationship

public class data2

{

       public string A_Abc { get; set; }
        public string A_cde { get; set; }
        public string A_efg{ get; set; }

}

source table

column name : abc,bcd,egf

destination table:

column name: A_abc,A_bcd,A_egf

error:-

'Data table requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'

 

after adding Hasnokey in Data2DBcontext.cs(destination.cs)

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
          base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Data>().HasNoKey();
        }

 

again getting invalid column names error


Answers (4)