0

Hey guys, I have an issue whereby only my parent is being inserted into DB, am i missing something? heres my code snippet

public partial class linqtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyContext db = new MyContext("Server=(local);Database=Test;User ID=admin;Password=pw");
            Child c = new Child();
            c.name = "joe "+DateTime.Now;
            db.parents.InsertOnSubmit(c);
            db.SubmitChanges();
        }
    }

    public class MyContext : DataContext
    {
        public static DataContext db;
        public MyContext(string connection) : base(connection) { }
        public Table<Parent> parents;
    }

    [Table(Name="parents")]
    [InheritanceMapping(Code = "retarded", Type = typeof(Child), IsDefault=true)]
    public class Parent
    {
        [Column(Name = "id", IsPrimaryKey = true, CanBeNull = false, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)] /* I want this to be inherited by subclasses */
        public int id { get; set; }

        [Column(Name = "name", IsDiscriminator = true)] /* I want this to be inherited by subclasses */
        public string name { get; set; }
    }

    public class Child : Parent
    {

    }
williamsandonz
  • 15,864
  • 23
  • 100
  • 186
  • Can you clarify what is in the `parents` table in your database after the insert? Both `Child` and `Parent` are being persisted to the same table (`parents`), so it's not clear what you mean that only the parent is being saved. What are you getting and what are you exepcting to get instead? – KP Taylor Apr 13 '11 at 03:30
  • oh sorry for lack of info, in the parents table i get id:(auto incremented by SQL), name: "retarded", I have another table children, which i want to tie to the class Child. But yea I haven't integrated this in yet cause I don't know how :-/ I tried to add [Table] to the calss Child but it errored as I had defined 2 root heirachys. EDIT: CRAP!! sorry for the retarded thing thats not suppose to be there – williamsandonz Apr 13 '11 at 03:39
  • There's your problem, unforuntately...answer forthcoming. :-) – KP Taylor Apr 13 '11 at 03:43

1 Answers1

0

What you are describing where you have have one table for a Parent object and another table for a Child object is called "multiple table inheritance" or "table per subtype"...and, unfortunately, it is not supported by Linq-to-SQL. See the SO question Multiple Inheritance in LINQtoSQL? and my own question looking for work-arounds at Simulating Multiple Table Inheritance in Linq-to-SQL. You can also Google "table inheritance in linq-to-sql" to see how to do single table inheritance in Linq-to-SQL, which is supported.

Community
  • 1
  • 1
KP Taylor
  • 2,100
  • 1
  • 17
  • 15
  • OH i seee!!!!!!!! Thanks alot for ur reply that's really helpful. :)! Now I'm unsure if I'll go to the Entity framework or not, I guess I'd like to but have just learnt LINQ2SQL and not sure if I can be bothered re-learning entity. The problem with single table inheritance for me is I'm going to have a lot of records in my table, could be looking at 100K, was hoping to get things in separate tables to keep search times down. I'll be indexing heavily, but if I leave in a single table, my lookups might become too slow :-( – williamsandonz Apr 13 '11 at 03:55
  • Yeah, I'm not happy with it myself, which I why I am working on finding some alternative. There *has* to be a way to do it relatively painlessly.... – KP Taylor Apr 13 '11 at 10:32