Base solution for your next web application
Open Closed

Entity Primary Key #3199


User avatar
0
pankajmathur created

Hi,

We have existing database. Each table has a PrimaryKey column like Below: Table: CusCustomer Field: CustomerKey - long - PrimaryKey

Table: InvProduct Field: ProductKey - long - PrimaryKey.

Now, if a create an entity class and derive it from FullAuditedEntity<Int64>, it will assume that there would be Id field in the table having datatype as long.

How can we create an entity having above table structure?

Regards, Mahendra

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can map Id field to your CustomerKey or ProductKey in the onModelCreating of your DbContext.

    Thanks.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    pankajmathur created

    Hi,

    Thanks for your reply.

    One more question. How Can I get rid of Identity Column that it creates by default. I want to specify my own value in this field from service. Something like below.

    public async Task CreateTaxCode(CreateTaxCodeInput input)
            {
                long abc = await _iVendRepository.GetNextPrimaryKeyValue();
                input.Id = abc; 
                var TaxCode = input.MapTo<TaxCode>();
                await _TaxCodeRepository.InsertAsync(TaxCode);
            }
    

    Currently, due to Identity column, it discards my value that I set in the Id column.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use something like this:

    modelBuilder.Entity<Entity>().Property(t => t.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)