pammu
pammu

Reputation: 31

Linq Insert: The column name ' ' is specified more than once in the SET clause or column list of an INSERT

I am using EF, I am not able to insert a data into table.

Question class look like this

[Table("QUESTION")]    
public class QUESTION
{      
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    public int q_id { get; set; }

    public string questname { get; set; }

    public string optionname { get; set; }
} 

Insert method

public string Save(QUESTION newquestion)
{
    try
    {
        using (dbcontextdata Context = new dbcontextdata())
        {
            QUESTION dbquest = new QUESTION();
            dbquest.id = newquestion.id;
            dbquest.q_id  = newquestion.q_id;
            dbquest.questname  = newquestion.questname;
            dbquest.optionname  = newquestion.optionname;
            Context.QUESTIONs.Add(dbquest);
            Context.SaveChanges();
        }
    }
    catch (Exception e)
    {
        return e.Message;
    }
    return "OK";
}

when I post the data, it shows error in SaveChanges()

The column name 'q_id' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code.

Here q_id is FK, help me.

Upvotes: 3

Views: 4309

Answers (2)

Morteza Jangjoo
Morteza Jangjoo

Reputation: 1790

[Index(IsUnique=true)]
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int q_id { get; set; }

Upvotes: 2

Felix Too
Felix Too

Reputation: 11931

You need to explicitly define q_idas a foreign key like the following.

 [ForeignKey("MyForeignTable")]
 public int q_id { get; set; }

Upvotes: 2

Related Questions