Hi,@Volk Volk
Accroding to your description
is it possible to link Primary Key in two tables Table_B with Table_A so that they match when creating a record in Table_B? I just don't want to create a separate model (extend Model), and then when writing to Table_B, copy all the properties from Model to NewModel (clone).
you may try with Table splitting
Create an Entity:
public class MyEntity
{
public int Id { get; set; }
public string? Prop1 {
get { return Prop1Copy; }
set { Prop1Copy = value; }
}
public string? Prop1Copy { get ; private set; }
// In my case,Prop1Copy would share the same value with Prop1,when you create/delete a record
//in MyEntity table,the record in MyEntityTableCopy table would also be created/deleted
// you could modify the getter/setter here and create a method to clone the properties
}
Configure as below in dbcontext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>
(
eb =>
{
eb
.ToTable("MyEntity",
tablebuilder=> {
tablebuilder.Property(x=>x.Id).HasColumnName("Id");
tablebuilder.Property(x => x.Prop1).HasColumnName("Prop1");
})
.SplitToTable("MyEntityTableCopy",
tablebuilder => {
tablebuilder.Property(x => x.Id).HasColumnName("Id");
tablebuilder.Property(x => x.Prop1Copy).HasColumnName("Prop1Copy");
});
}
);
}
Result
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
RuikaiFeng