Base solution for your next web application
Open Closed

Database migration #1406


User avatar
0
vitaly created

**I have 2 questions related to database migration: 1 - I create entities and declare that ID should be called like EntityID, but when the migration script is generated there are two fileds the ID and the EntityID in target Table. See script below, marked in red. Question: why and how to get rid of ID filed?

2 - Two entities are related, I want the relation field to be called the same way in both tables, like EntityID. But when the migration script is generated the field is called Entity_EntityId. See script below, marked green. Question: how and where to override this convention.**

ENTITY #1 [Table("OmniCategories")] public class Category : FullAuditedEntity, IMustHaveTenant { public Category() { Items = new HashSet<Item>(); }

    public virtual int TenantId { get; set; }              

    [Key]
    public virtual int CategoryId { get; set; }             

    public virtual int? ParentCategoryId { get; set; }     

    [ForeignKey("ParentCategoryId")]
    public virtual Category Parent { get; set; }            

    public virtual ICollection&lt;Item&gt; Items { get; set; }    
}

ENTITY #2 [Table("OmniItems")] public class Item : FullAuditedEntity, IMustHaveTenant { public virtual int TenantId { get; set; }

    [Key]
    public virtual int ItemId { get; set; }

    public virtual int? ParentItemId { get; set; }

	[Required]
    [Column("CategoryId")]
    public virtual Category Category { get; set; }

    [ForeignKey("ParentItemId")]
    public virtual Item Parent { get; set; }

}

MIGRATION CreateTable( "dbo.OmniItems", c => new { ItemId = c.Int(nullable: false, identity: true), TenantId = c.Int(nullable: false), ParentItemId = c.Int(), IsDeleted = c.Boolean(nullable: false), DeleterUserId = c.Long(), DeletionTime = c.DateTime(), LastModificationTime = c.DateTime(), LastModifierUserId = c.Long(), CreationTime = c.DateTime(nullable: false), CreatorUserId = c.Long(), <span style="color:#FF0000"> Id = c.Int(nullable: false),</span> Category_CategoryId = c.Int(nullable: false),

                },
            annotations: new Dictionary&lt;string, object&gt;
            {
                { "DynamicFilter_Item_MustHaveTenant", "EntityFramework.DynamicFilters.DynamicFilterDefinition" },
                { "DynamicFilter_Item_SoftDelete", "EntityFramework.DynamicFilters.DynamicFilterDefinition" },
            })
            .PrimaryKey(t => t.ItemId)
            .ForeignKey("dbo.OmniCategories", t => t.&lt;span style=&quot;color:#00BF00&quot;&gt;Category_CategoryId&lt;/span&gt;, cascadeDelete: true)
            .ForeignKey("dbo.OmniItems", t => t.ParentItemId)
            .Index(t => t.ParentItemId)
            .Index(t => t.Category_CategoryId);

2 Answer(s)
  • User Avatar
    0
    vitaly created

    Ok, i fixed point 2 by myself. But point 1 I can't fix.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You have an Id field because your entities are derived from Entity class, You can define another base entity class for yourself like EntityBaset than override Id field in it like this,

    [Column("EntityId")]
    public override long Id {get; set;}
    

    And you should derive all of your entities from EntityBase.