Why is EF trying to insert NULL in id-column?

前端 未结 9 1986
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 00:43

I am writing my project using Entity Framework 4.0 (Model first). At the beginning of the project, I faced with this problem: I am trying to insert the filled object in the

相关标签:
9条回答
  • 2020-12-10 01:29

    I ran into this today and had to regenerate my EF classes from the database.

    After doing that, I found that EF added:

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

    to this"Id" field that used to be an Identity column in the SQL but was changed to be app-assigned.

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

    0 讨论(0)
  • 2020-12-10 01:31

    0

    İf you dont use code first or db first oracle dont create be sequnce You try it trigger with sequence

         CREATE OR REPLACE TRIGGER 
    "a"."IHALEYOL"
    before insert on "a"."IHALEYOL"
    for each row
    begin
      select "a"."SQ_DIREK".nextval into :new."ID" from dual;
    end;
    

    seq:

     create sequence SQ_YETKI
    minvalue 1
    maxvalue 9999999999999999999999999999
    start with 221
    increment by 1
    cache 20;
    
    0 讨论(0)
  • 2020-12-10 01:32

    I had created the table with int Id as PK, but had forgotten to set "Identity Specification" = True

    0 讨论(0)
提交回复
热议问题