LINQ to SQL插入主键索引 [英] LINQ to SQL insert primary key index

查看:75
本文介绍了LINQ to SQL插入主键索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Column(Name = "id", IsPrimaryKey = true, CanBeNull = false)] 
public string id { get; set; }

错误:

无法将值Null插入 列ID,列不允许为空

Cannot insert the value Null into column id, column does not allow nulls

如果我将其设置为标识列,并在[Column attributes]中进行了一些更改,最后遇到未启用IDENTITY插入的错误,该怎么办?我是否还需要主键列作为身份标识?

If I set it to an identity column and make a few changes in my [Column attributes] I end up with a IDENTITY insert not enabled error, what should I do? Do I need my primary key column to be an identity as well?

这只是一个简单的表,主键+1递增

It's just a simple table with +1 increment on primary key

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
    {

    }

推荐答案

如果我仅将ID列定义为IsPrimaryKey = trueIsDbGenerated = true,则一切正常:

If I just define my ID column as IsPrimaryKey = true and IsDbGenerated = true, everything works just fine:

[ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, 
 DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int ID
{....}

然后,在代码中-创建新实体时,我只是为该实体分配任意ID-我通常倾向于使用负数:

Then, in code - when I create a new entity, I just assign the entity an arbitrary ID - I typically tend to use negative numbers:

MyEntity entity = new MyEntity { ID = -55 };

MyContext.MyEntities.InsertOnSubmit(entity);
MyContext.CommitChanges();

在调用.CommitChanges()之后,新实体存储在数据库表中,并且实际的实际ID值(INT IDENTITY)被设置并反映在我的entity对象上-即在调用,我得到了数据库赋予我的新实体的真实ID.

After the call to .CommitChanges(), the new entities are stored in the database table, and the actual, real ID values (INT IDENTITY) are being set and reflected on my entity object - i.e. after the call, I get back the real ID that the database has given my new entity.

所以我的问题是:那你的问题在哪里?看来一切正常....

So my question is: where is your problem then?? It seems to work just fine....

这篇关于LINQ to SQL插入主键索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆