1

This is the error i get when I'm trying to save data to my table.

Error:

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

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

Controller Code:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="custid,name,cell_No,address,date,time,serviceNo")] CustormerD custormerd)
{
    if (ModelState.IsValid)
    {
        db.CustormerDs.Add(custormerd);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(custormerd);
}  

table code below:

public partial class CustormerD
{
    public int custid { get; set; }
    public string name { get; set; }
    public Nullable<int> cell_No { get; set; }
    public string address { get; set; }
    public Nullable<System.DateTime> date { get; set; }
    public Nullable<System.TimeSpan> time { get; set; }
    public Nullable<int> serviceNo { get; set; }
}

sql code

 CREATE TABLE [dbo].[CustormerD](
[custid] [int] IDENTITY(1,1) NOT NULL,
[name] [nchar](20) NULL,
[cell_No] [int] NULL,
[address] [nchar](20) NULL,
[date] [date] NULL,
[time] [time](7) NULL,
[serviceNo] [int] NULL
) ON [PRIMARY]
sibonile
  • 109
  • 1
  • 1
  • 10

2 Answers2

1

As per discussed in comment OP use .edmxfile.

So you can set primary key to your table like following:

USE [yourdatbase]
GO

/****** Object:  Table [dbo].[CustormerD]    Script Date: 26/09/2016 4:08:23 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[CustormerD](
    [custid] [int] IDENTITY(1,1) NOT NULL,
    [name] [nchar](20) NULL,
    [cell_No] [int] NULL,
    [address] [nchar](20) NULL,
    [date] [date] NULL,
    [time] [time](7) NULL,
    [serviceNo] [int] NULL,
 CONSTRAINT [PK_CustormerD] PRIMARY KEY CLUSTERED 
(
    [custid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

After set the primary key, you should update your .edmx model from the database.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
0

The Entity Framework will only assume a primary key from your model if the name of the field is the name of the model + ID. Otherwise you will have to explicitly state which field to use as a primary key. For your model that would mean either changing the name of your primary key to:

public int CustomerDID { get; set; }

Entity framework would then automatically assume it as the primary key. Alternatively you label your primary key using [Key] as follows:

    public partial class CustormerD
{
    [Key]
    public int custid { get; set; }
    public string name { get; set; }
    public Nullable<int> cell_No { get; set; }
    public string address { get; set; }
    public Nullable<System.DateTime> date { get; set; }
    public Nullable<System.TimeSpan> time { get; set; }
    public Nullable<int> serviceNo { get; set; }
}
Rob
  • 199
  • 21