Davood
Davood

Reputation: 5645

insert data in no identity number column

I have a table in my database.

This table has one column named A, the column is not entity or auto number, unique key or...

I created a model from my db with Entity Framework 4.1.

My code

 using (var contex = new testEntities())
        {
            Table_1 t = new Table_1();
            t.A = 1;
            contex.Table_1.Add(t);
            contex.SaveChanges();
        }

I do not want to use the identity number or index in my table. When I want to insert a row in it it gives me this error:

Unable to update the EntitySet 'Table_1' because it has a DefiningQuery and no 
<InsertFunction> element exists in the <ModificationFunctionMapping> element to
support the current operation.

Upvotes: 0

Views: 253

Answers (1)

Colin Mackay
Colin Mackay

Reputation: 19185

You either have to give the table a primary key (which you don't seem to want to do) or you need to provide a function to tell Entity Framework how data can be inserted into the database.

I would strongly suggest that you put a primary key on your table unless you have a very good reason for not doing this, and in all my years I can't think of many instances when I wanted a table without a primary key.

If you don't want the primary key at all, then you might find this article useful about using Stored Procs for INSERTing data into the database: http://msdn.microsoft.com/en-us/data/gg699321.aspx

Upvotes: 1

Related Questions