2016-09-26 15 views
1

これは、データをテーブルに保存しようとしたときに発生するエラーです。私はMVCで足場を作成しました。作成ボタンをクリックすると、フォームにデータを入力した後、以下のエラーが発生します。

エラー:

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.

コントローラコード:

[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); 
} 

下表コード:

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コード

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] 
+0

テーブルにプライマリキーがない場合、このエラーが発生する可能性があります。 –

+2

'custid'に' '[Key]'を追加してください –

+0

私のデータベーステーブルで何が失敗したのか教えてください。 – sibonile

答えて

1

だから、次のようなあなたのテーブルに主キーを設定することができます。主キーを設定し

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 

したら、データベースからあなた.edmxモデルを更新する必要があります。

0

フィールドの名前がモデル+ IDの名前である場合、Entity Frameworkはモデルからの主キーのみを引き継ぎます。そうしないと、プライマリキーとして使用するフィールドを明示的に指定する必要があります。あなたの主キーの名前を変更するのいずれかを意味しますあなたのモデルの場合:

public int CustomerDID { get; set; } 

エンティティフレームワークが自動的に主キーとして、それを前提とします。別の方法としては、次のように[キー]を使用して、主キーにラベルを付ける:コメントのOPの使用で説明.edmxファイルを1として

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; } 
} 
関連する問題