linq to sql only parent being inserted into db
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.W开发者_如何学Ceb.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
{
}
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.
精彩评论