Data Migration Files didn't generated with specific case

Topics: Writing modules
Nov 2, 2011 at 1:33 PM

Hi ,

I am creating an module, when i am going to generate data migration file with related data model, it will be generated but if i define property name different its only generates the data migartion for first class, mention below code

This part generates the data migration correctly

namespace BRIJ.Brij.Models
{
    public class BrijRecord
    {
        public virtual int Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
   }   
    public class FirstBrijChildRecord
    {
        public virtual int  Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
        public virtual BrijRecord ParentID { get; set; }
    }
      
    public class SecondBrijChildRecord
    {
        public virtual int  Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
        public virtual FirstBrijChildRecord ParentID { get; set; }
    }
      
    public class ThirdBrijChildRecord
    {
        public virtual int  Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
        public virtual SecondBrijChildRecord ParentID { get; set; }
    }
   } 

 

But if i change the property name as following it will generate the data migration for first only

 

namespace Brij.Brij2.Models
{
    public class Brij2Record
    {
        public virtual int Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
   }   
    public class FirstBrij2ChildRecord
    {
        public virtual int  FirstId { get; set; }
        public virtual String FirstName { get; set; }
        public virtual String FirstValue { get; set; }
        public virtual Brij2Record FirstParentID { get; set; }
    }
      
    public class SecondBrij2ChildRecord
    {
        public virtual int  SecondId { get; set; }
        public virtual String SecondName { get; set; }
        public virtual String SecondValue { get; set; }
        public virtual FirstBrij2ChildRecord SecondParentID { get; set; }
    }
      
    public class ThirdBrij2ChildRecord
    {
        public virtual int  ThirdId { get; set; }
        public virtual String ThirdName { get; set; }
        public virtual String ThirdValue { get; set; }
        public virtual SecondBrij2ChildRecord ThirdParentID { get; set; }
    }
   }

 

The data migration file output is following

 

 public class Migrations : DataMigrationImpl {

        public int Create() {
            // Creating table Brij2Record
            SchemaBuilder.CreateTable("Brij2Record", table => table
                .Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())
                .Column("Name", DbType.String)
                .Column("Value", DbType.String)
            );



            return 1;
        }
    }

 

Thanks 

Brijender

Propeersinfo

www.propeersinfo.com

 

Nov 2, 2011 at 2:59 PM

One problem I see is that you are suffixing "ID" on fields that are *not* IDs. So it should be:

        public virtual Brij2Record FirstParent { get; set; }

If that was an int field that represent the Id of another record, it would be correct to suffix Id - but since it's representing the actual *record*, it's not an Id (and NHibernate will generate an Id column for you behind the scenes, you don't need to explicitly create it).

By suffixing Id on fields that aren't Ids, you could be confusing the migrations generator.

Nov 3, 2011 at 9:30 AM

That field is int representing the ID , so we can't remove it, and it did,'t help

Nov 3, 2011 at 3:24 PM
Edited Nov 3, 2011 at 3:24 PM

No it's not an int, it's an object. Look:

 

   public virtual Brij2Record FirstParentID { get; set; }

 

That's a property of type "Brij2Record" - not an int. So it's not an Id (the migration will add an id column for you in the database, called FirstParent_id, but all you have to declare on the record class is the object property).

Nov 4, 2011 at 5:10 AM

Hi Thanks for reply

 

yes, i implemented that as  well, forget to mention in last post,

 public class BrijRecord
    {
        public virtual int Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
   }   
    public class FirstBrijChildRecord
    {
        public virtual int FirstId { get; set; }
        public virtual String FirstName { get; set; }
        public virtual String FirstValue { get; set; }
        public virtual int FirstParentID { get; set; }
    }
      
    public class SecondBrijChildRecord
    {
        public virtual int SecondId { get; set; }
        public virtual String SecondName { get; set; }
        public virtual String SecondValue { get; set; }
        public virtual int SecondParentID { get; set; }
    }
      
    public class ThirdBrijChildRecord
    {
        public virtual int ThirdId { get; set; }
        public virtual String ThirdName { get; set; }
        public virtual String ThirdValue { get; set; }
        public virtual int ThirdParentID { get; set; }
    }

the out comes is same

Nov 4, 2011 at 6:21 AM

ahh resolved,

  public class BrijRecord
    {
        public virtual int Id { get; set; }
        public virtual String Name { get; set; }
        public virtual String Value { get; set; }
   }   
    public class FirstBrijChildRecord
    {
        public virtual int Id { get; set; }
        public virtual String FirstName { get; set; }
        public virtual String FirstValue { get; set; }
        public virtual BrijRecord FirstParent { get; set; }
    }
      
    public class SecondBrijChildRecord
    {
        public virtual int Id { get; set; }
        public virtual String SecondName { get; set; }
        public virtual String SecondValue { get; set; }
        public virtual FirstBrijChildRecord SecondParent { get; set; }
    }

Nov 4, 2011 at 7:53 AM

Yep, that's what I meant.

You can use int id fields in addition to the objects, but the normal convention is to name them like this: ThirdParent_id (otherwise it might not work properly)