Can I set my PrimaryKey to Model before adding it into the Table (_db.Table.Add(Model);)?

Volk Volk 551 Reputation points
2023-08-13T14:18:27.6833333+00:00

Hi!

I have a Table_A table, it has a unique Primary Key. The model that writes there, for example, is: Model {Id (Primary Key), a, b, c, d}. I need to keep a copies of Models (from Table_A) somewhere (in an independent Table_B table), but so that this copy is created (updated) when I need it, AND HAS the SAME Id(Primary Key). Is it possible to implement this in net.core or maybe there is another way? How do I create a Table_B table -with Id(Primary Key) or just with Id (to specify Id manually when recording)? Most importantly, I need to use the same Model in operations with tables Table_a and Table_B.

Is it possible to implement this and how? Or are there more elegant ways?

P.S. Here, for example, they write that it is impossible to create a table without PrimaryKey at all.
Can we have table without primary key in entity framework?
https://stackoverflow.com/questions/15381233/can-we-have-table-without-primary-key-in-entity-framework
Then how do I set manually PrimaryKey to Table_B, which stores the same Model?

Maybe, 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). Or is there a Clone() command for such a case?

Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,761 Reputation points Microsoft External Staff
    2023-08-14T02:30:05.88+00:00

    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");
                          });
                   }
                );
        }
    

    Result8.14


    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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.