{"不能插入时IDENTITY_INSERT设置为OFF表'中心联系“标识列显式值"} [英] {"Cannot insert explicit value for identity column in table 'Cantact' when IDENTITY_INSERT is set to OFF."}

查看:174
本文介绍了{"不能插入时IDENTITY_INSERT设置为OFF表'中心联系“标识列显式值"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存在我的计划联系人是一个简单的电话本在C#中,我使用LINQ和放大器;实体框架和放大器;当我想添加或更改任何数据,我得到一个运行时错误




不能在表中插入的标识列的显式值联系我们的时候IDENTITY_INSERT设置为OFF。




下面是我的插入(添加)代码,在另一方面,我不希望在我的主键是ID添加任何数据,我想把它留给我的SQL Server。



感谢大家帮助我

 公共无效保存()
{
使用(VAR的Contex =新Phone_BookEntities1())
{
变种p值=从C在Contex.Cantacts
,其中c.Cantact1 ==名称
选择新的{c.Cantact1,c.Number};

如果
{
Ref_Cantact =新中心联系()(p.Any()!);
Ref_Cantact.Cantact1 =名称;
Ref_Cantact.Number =民;
Contex.Cantacts.Add(Ref_Cantact);
Contex.SaveChanges();
}
}
}

修改

 公共部分类中心联系
{
公共字符串Cantact1 {搞定;组; }
公共串号{搞定;组; }
公众诠释ID {搞定;组; }
}


解决方案

你可以试试这个:这将包装在一个事务中的所有电话,因此设置标识插入上插入语句(调用时添加+的SaveChanges由EF创建)。

 如果
{
Ref_Cantact =新中心联系()(p.Any()!);
Ref_Cantact.Cantact1 =名称;
Ref_Cantact.Number =民;
使用(VAR反式= Contex.Database.BeginTransaction())
{
Contex.Database.ExecuteSqlStatement(SET IDENTITY_INSERT联系ON;);
Contex.Cantacts.Add(Ref_Cantact);
Contex.SaveChanges();
trans.Commit();
}
}



编辑:另一种可能性是设置自动递增(DatabaseGeneratedOption。身份)了,(在上下文类(或徘徊无论你的模型构建器))使用:

  modelBuilder.Entity< Cantacts>() .Property(X => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 


I'm trying to save a contact in my program which is a simple phone book in C# and I'm using linq & Entity Framework & when I want to add or change any data I get a run time error

Cannot insert explicit value for identity column in table 'Contact' when IDENTITY_INSERT is set to OFF.

Here's my insert (add) code, on the other hand I don't want to add any data in my primary key which is ID and I want to leave it to my SQL Server.

Thank you all for helping me

public void Save()
{
    using (var Contex = new Phone_BookEntities1())
    {
        var p = from c in Contex.Cantacts
                where c.Cantact1 == Name
                select new { c.Cantact1, c.Number };

        if (!p.Any())
        {
            Ref_Cantact = new Cantact();
            Ref_Cantact.Cantact1 = Name;
            Ref_Cantact.Number = Num;
            Contex.Cantacts.Add(Ref_Cantact);
            Contex.SaveChanges();
        }
    }
} 

EDIT

public partial class Cantact 
{ 
    public string Cantact1 { get; set; } 
    public string Number { get; set; } 
    public int ID { get; set; } 
} 

解决方案

you can try this: this will wrap all calls in a transaction, therefore setting identity insert on for the insert statement (Created by EF when calling Add+SaveChanges).

if (!p.Any())
            {
                Ref_Cantact = new Cantact();
                Ref_Cantact.Cantact1 = Name;
                Ref_Cantact.Number = Num;
                using(var trans=Contex.Database.BeginTransaction())
                {
                    Contex.Database.ExecuteSqlStatement("SET IDENTITY_INSERT Contact ON;");
                    Contex.Cantacts.Add(Ref_Cantact);
                    Contex.SaveChanges();
                    trans.Commit();
                }
            }

EDIT: Another possibility would be setting AutoIncrement (DatabaseGeneratedOption.Identity) off, using (in your modelbuilder in context class (or whereever)):

modelBuilder.Entity<Cantacts>().Property(x=>x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

这篇关于{&QUOT;不能插入时IDENTITY_INSERT设置为OFF表'中心联系“标识列显式值&QUOT;}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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