Base solution for your next web application
Open Closed

Overriding ID field for legacy table compatibility #11070


User avatar
0
Kaarlaid created

We are developing an ASPNetZero solution to augment an existing desktop + MS Sql Server solution, and are going through the process of using the RAD Tool to build table structure that match, as closely as possible, the existing desktop solution. After this is done we will use a replication process to transfer records between the desktop (local) and ASPNetZero (remote) databases.

The problem we have is that the desktop solution has been developed using Developer Expresses XPO (as an ORM), and it creates the primary key with field name OID, eg:

For the replication process to be as simple as possible to implement I'm - based on information from another ticket - trying to create the following structure for our ASPNetZero solution:

 [Table("City")]
    public class City : Entity, IMayHaveTenant
    {
        [NotMapped]
        public override int Id
        {
            get { return OID; }
            set { /* nothing */ }
        }

        public int? TenantId { get; set; }

        [Required, Key]
        public virtual int OID { get; set; }

        public virtual int? CreatingUser { get; set; }

        public virtual int? LastEditedUser { get; set; }

        public virtual int? Country { get; set; }

        [StringLength(CityConsts.MaxNameLength, MinimumLength = CityConsts.MinNameLength)]
        public virtual string Name { get; set; }

        public virtual int? OptimisticLockField { get; set; }

        public virtual int? GCRecord { get; set; }

    }

Firstly - does this seem a correct approach to you? New "City" records, eventually, need to be created within the ASPNetZero solution (and replicated to the ''local" database), as well as continuing to be created within the "local" database (and replicated to the remote / ASPNetZero solution).

Nextly - when I try and apply a migration based on the above structure I get this error message: "To change the IDENTITY property of a column, the column needs to be dropped and recreated." ... which I guess is related to the "OID" column.

I was wondering if you had any experience or advice to offer with this scenario. I guess another solution is to just override the ID field, and make it non-autogenerated, then use our replication code to handle the difference in table structures (with OID on the local database side, and ID on the remote ASPNetZero side).


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

    Hi,

    Could you try this approach instead of adding a new field ?

    [Table("City")]
    public class City : Entity, IMayHaveTenant
    {
    	[Column("OID")]
    	public override int Id { get; set; }
    
    	public int? TenantId { get; set; }
    
    	// [Required, Key]
    	// public virtual int OID { get; set; }
    
    	public virtual int? CreatingUser { get; set; }
    
    	public virtual int? LastEditedUser { get; set; }
    
    	public virtual int? Country { get; set; }
    
    	[StringLength(CityConsts.MaxNameLength, MinimumLength = CityConsts.MinNameLength)]
    	public virtual string Name { get; set; }
    
    	public virtual int? OptimisticLockField { get; set; }
    
    	public virtual int? GCRecord { get; set; }
    
    }
    
  • User Avatar
    0
    Kaarlaid created

    Thanks! That works for a migration, and will make the replication easier. I think I also need to add the the "DatabaseGeneratedOption.None" property, though, like so:

    [Column("OID"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public override int Id { get; set; }
    

    ... as the OID coming from the legacy system will need to be used by the replication code, rather than having an auto-generated value. Then I just need to set the OID explicitly when we start creating data within the ASPNetZero solution.

    Thanks for your help! :)