none
Unexpected behavior in EF Code first, FK is taken from previous statement automatically by child table RRS feed

  • General discussion

  • Hi, this is my model :
    class model2 : DbContext
        {
            public DbSet<Tab> Tabs { get; set; }
            public DbSet<ChildTab> ChildTabs { get; set; }
            public model2()
            {
              
                Database.SetInitializer(new DropCreateDatabaseIfModelChanges<model2>());
            }
         
            public model2(string conn)
                : base(conn)
            {
                Database.SetInitializer(new DropCreateDatabaseIfModelChanges<model2>());
            }
        }
        public class Tab
        {
            [Key]
            public int kcol { get; set; }
            public long int64 { get; set; }
            public string col { get; set; }
        }
        public class ChildTab
        {
            [Key]      
            public int kcol { get; set; }
            public long int64 { get; set; }
            public string col { get; set; }
            public Tab parenttab { get; set; }
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
            public int Tabkcol { get; set; } //will be FK 
        }
    and here is my code:
       model2 ctx = new model2("EF_Oracle_11g_UTF16");
                    ctx.Tabs.Add(new Tab {  });
                    ctx.ChildTabs.Add(new ChildTab {  }); // this will automatically insert above object as parent, if we comment above statement it will throw error
    I am expecting a exception here, like "parent key not found"(because the Tabkcol value will be 0 by default), but it doesn't throw.
    Is this is something by design or unexpected behavior?
    Friday, August 19, 2011 5:46 AM

All replies

  • Did any one look in to this? 

    I will be happy to provide more clear info on this, if required.


    Jagan
    Monday, November 28, 2011 4:57 AM