[Column(Name = "id", IsPrimaryKey = true, CanBeNull = false)]
public string id { get; set; }
Cannot insert the value Null into column id, column does not allow nulls
如果我将其设置为标识列,并在我的[Column attributes]
我结束了一个IDENTITY一些变化嵌件未启用的错误,是什么我应该怎么做?我是否也需要我的主键列作为身份?
这只是一个简单的表+1增量的主键
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
{
}
你为什么设置主键?我的意思是不是你正在使用的数据库提供主键? – Bastardo 2011-04-12 20:58:36
我不需要设置isPrimaryKey吗?我以为我做了 – Baconbeastnz 2011-04-12 23:40:08
好吧,如果你有一个数据库和一些表格,并且使用Sql Server或者Oracle等等你不需要。你可以在这些程序中设置它们,并在你的表中提供主键。 – Bastardo 2011-04-13 03:48:33