English 中文(简体)
为什么欧洲论坛试图在col中插入全国扫盲运动?
原标题:Why is EF trying to insert NULL in id-column?

我是利用实体框架4.0(模块一)撰写我的项目。 在项目开始时,我面临这一问题: 我正试图在数据库中添加填满的物体,但我已感动:

不能将NUL列入Id栏,表BDB.dbo。 页: 1 INSERT失败。 声明已终止。

法典:

    Category usingCategory = new Category("Using Forums", "usingforums", 0);
    using (Context)
    {
        Context.Categories.AddObject(usingCategory);
        Context.SaveChanges();
    }

我检查了这个物体,我确信该物体已经填满。

例如:

public Category(string name, string urlName, int index)
{
    CategoryId = Guid.NewGuid();
    Name = name;
    UrlName = urlName;
    CategoryIndex = index;
}

正在发生什么?

最佳回答
问题回答

我今天已经进入这一阶段,不得不从数据库中恢复我的EF课程。

在这样做之后,我发现欧洲复兴开发银行补充说:

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }

这里的“Id”领域曾经是KQ中一个身份栏,但后来改换成签名。

I think if you don t have that attribute EF won t actually send the ID to the database ( convention over configuration )

我用Id作为PK制作了表格,但忘记了确定“实体规格”=真实性。

Try to add this into your model class .cs file:

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CategoryId { get; set; }

或更改类别 身份:

    CategoryId int IDENTITY(1,1)

For me I had a different schema other than default one. The name of the Id key was some how missing the that schema part - fixed that in the database and it all went well. How it was ;

PK_TableName

我是如何改变的。

PK_mySchema.TableName

第 一 章

[Key]
 [DatabaseGenerated(DatabaseGeneratedOption.None)]
 public Int64 PolicyID { get; set; }

今天也处理同样的问题。

这里,我是如何阐述这一问题的。

I had added the identity seed to the column initially but after that I removed it. So I did not realize that when modifying the column definition in the Code first, the Id

Property(x => x.Id)
    .HasColumnName("Id")
    .HasColumnType("int")
    .IsRequired();

因此,实体框架显示,它是一个身份栏,我是上述例外。

为了确定这一点,我补充说:

HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)

因此,最后一点是:

Property(x => x.Id)
    .HasColumnName("Id")
    .HasColumnType("int")
    .IsRequired()
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签