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().HasNoKey();
}
again getting invalid column names error
Tuhin PaulPosted Mar 26, 2023, 9:40 AM
Once you have the data in the destinationData list, you can insert it into the destination table using the AddRange method of the DbContext class.
This will insert all the data from the destinationData list into the Data2 table.
Tuhin PaulPosted Mar 26, 2023, 9:39 AM
To insert data from a source to a destination table in Entity Framework Core with different column names and no primary key, you can use a combination of LINQ and mapping to achieve this. Fetch the data from the source table and map it to the destination table.
Meghana MPosted Mar 25, 2023, 11:16 AM
after using fluent API
.HasNoKeyto define the keyless entity issue is resolved but again getting another error invalid column namesource table
column name : abc,bcd,egf
destination table:
column name: A_abc,A_bcd,A_egf
Ali BenchaabanPosted Mar 25, 2023, 11:00 AM
Hello Meghana M ,
To define Keyless Entities in EF, there are two ways : based in your example
1- Use data annotation
[Keyless]. Annotate a model with this attributeOR
2- use fluent API
.HasNoKeyto define the keyless entityBENCHAABAN Ali