Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Is it possible to switch off the Identity Column? #1190


User avatar
0
tjackadams created

Hello,

Is it possible to switch off the Identity Column for the provided Entities such as Tenants? We are looking to pre-populate the Tenants table with Id, TenancyName and Name from another table within our database but we would like to retain the existing Id's if possible.

I have tried editing the OnModelCreating method, which seems to work fine for Entities i have created myself, but not for the Tenant Entity.

If you can let me know if this is possible or just point me in the right direction that would be great. Thanks


2 Answer(s)
  • User Avatar
    0
    jawadsharif created

    Yes actually there are multiple ways to do that you can do this in your Tennant.cs file as

    /// <summary>
        /// Represents a Tenant in the system.
        /// A tenant is a isolated customer for the application
        /// which has it's own users, roles and other application entities.
        /// </summary>
        public class Tenant : AbpTenant<Tenant, User>
        {
            // hide/ override the Id property from base class FullAutitedEntity<int> and decorate it [DatabaseGenerated(DatabaseGeneratedOption.None)]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int Id { get; set; }
    
            protected Tenant()
            {
            }
    
            public Tenant(string tenancyName, string name)
                : base(tenancyName, name)
            {
            }
        }
    

    or also in OnModelCreating Method like

    modelBuilder.Entity<Tenant>()
                    .Property(p => p.Id)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    

    Hope This helps

  • User Avatar
    0
    tjackadams created

    Thanks! I was on the right lines with the modelBuilder but my syntax was slightly off.