linq to sql only parent被插入到db中

时间:2011-04-13 03:25:06

标签: linq-to-sql

嘿伙计们,我有一个问题,只有我的父母被插入数据库,我错过了什么?继承我的代码片段

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
    {

    }

1 个答案:

答案 0 :(得分:0)

你所描述的是你有一个父对象的表和另一个用于Child对象的表被称为“多表继承”或“每个子类的表”......而且不幸的是,Linq不支持它-to-SQL。请参阅问题Multiple Inheritance in LINQtoSQL?和我自己的问题,寻找Simulating Multiple Table Inheritance in Linq-to-SQL的解决方法。您还可以使用Google“linq-to-sql中的表继承”来查看如何在Linq-to-SQL中执行单表继承,这是受支持的。