【发布时间】:2011-04-13 03:25:06
【问题描述】:
大家好,我有一个问题,只有我的父母被插入到数据库中,我错过了什么吗?这是我的代码sn-p
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
{
}
【问题讨论】:
-
您能否澄清插入后数据库中
parents表中的内容?Child和Parent都被持久化到同一个表(parents),所以不清楚你的意思是只有父级被保存。你得到了什么,你期望得到什么? -
对不起,缺少信息,在父母表中我得到了 id:(由 SQL 自动递增),名称:“延迟”,我有另一个表孩子,我想把它与班级联系起来孩子。但是是的,我还没有将它集成到其中,因为我不知道如何:-/我试图将 [Table] 添加到 calss Child 但它出错,因为我定义了 2 个根 heirachys。编辑:废话!!对不应该存在的迟钝的东西感到抱歉
-
这是你的问题,不幸的是......答案即将到来。 :-)
标签: linq-to-sql