2017-04-04 27 views
0

Entity Framework 6を​​使用してテーブルを挿入および更新しようとしていますが、できません。Entity Framework 6でテーブルを追加または更新できません

私は挿入すると、それはDefiningQueryを持っており、どの要素が

すると、現在の操作をサポートするために、要素内に存在しないので、私はエラー

のEntitySet「SampleV2」を更新できません

を取得私は更新すると、投げられたエラーは以下の通りです:

プロパティ '名前'はオブジェクトのキー情報の一部であり、変更することはできません。

マイコード:

//Table script 
CREATE TABLE [dbo].[SampleV2](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [Name] [varchar](150) NOT NULL, 
    [DateOfBirth] [datetime] NOT NULL, 
    [Status] [bit] NOT NULL 
) ON [PRIMARY] 

// SampletV2.cs 
public partial class SampleV2 
{ 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public System.DateTime DateOfBirth { get; set; } 
     public bool Status { get; set; } 
} 

// Utilities.cs 
public static Dictionary<bool,string> SavePlayerDetails(SampleV2 model) 
{ 
      Dictionary<bool, string> dicInfo = new Dictionary<bool, string>(); 

      if (model.Id == 0) 
      { 
       try 
       { 
        SampleV2 sam = new SampleV2 
        { 
         Name = model.Name, 
         DateOfBirth = model.DateOfBirth, 
         Status = model.Status 
        }; 

        using (var _context = new ExamEntities1()) 
        { 
         _context.SampleV2.Add(sam); 
         _context.SaveChanges(); // getting error here 
        } 
       } 
       catch (Exception e) 
       { 
        throw; 
       }     
      } 
      else 
      { 
       try 
       { 
        using (var _context = new ExamEntities1()) 
        { 
         SampleV2 data = _context.SampleV2.SingleOrDefault(a => a.Id == model.Id); 
         data.DateOfBirth = model.DateOfBirth; 
         data.Name = model.Name; 
         data.Status = model.Status; 
         _context.Entry(data).State = System.Data.Entity.EntityState.Modified; // getting error here 
         _context.SaveChanges(); 
        } 
       } 
       catch (Exception e) 
       { 
        throw; 
       }     
      }    

      return dicInfo; 
} 
+0

DBと 'KeyAttribute'の主キーに' id'を追加してみてください。エラーはターゲットテーブルの主キーが失われていることを示しています。 –

+0

モデルが動的に作成されました。KeyAttributeを手動で追加したいですか? –

+0

複合PKをお持ちですか? –

答えて

0

あなたが最初にデータベースを使用している場合、エラーがSampleV2エンティティまたはテーブルに主キーフィールドが欠落していることを示しているか、それがモデルクラスに設定されていません。主キーフィールドとしてそれをマークするidエンティティにKeyAttributeを追加

試してみてください。

public partial class SampleV2 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public System.DateTime DateOfBirth { get; set; } 
    public bool Status { get; set; } 
} 

また、あなたがして、Set Primary Keyとテーブルデザイナを使用し、EDMXファイルにIdentityとしてStoreGeneratedPatternを設定するDBに主キーを追加する必要があります生成されたモデルをデータベースから更新し、マッピングされたエンティティクラスがID主キーフィールドを有することを保証する。

NB:KeyAttributeはT4テンプレート(.tt)を編集することによって自動的に生成することができる。このようなファイル:

<#=codeStringGenerator.UsingDirectives(inHeader: false)#> 
using System.ComponentModel.DataAnnotations; 
// -- other stuff 

var simpleProperties = typeMapper.GetSimpleProperties(entity); 
if (simpleProperties.Any()) 
{ 
    foreach (var edmProperty in simpleProperties) 
    { 
     if (ef.IsKey(edmProperty)) { 
    #> [Key] 
    <# } 
    #> 
    <#=codeStringGenerator.Property(edmProperty)#> 
    <# 
    } 
} 

同様の問題:

Unable to update the EntitySet - because it has a DefiningQuery and no <UpdateFunction> element exist

Entity Framework 4 Error: Unable to update the EntitySet because it has a DefiningQuery

Why am I getting a "Unable to update the EntitySet because it has a DefiningQuery..." exception when trying to update a model in Entity Framework?

+0

StoreGeneratedPatternはすでにIDのみに設定されています。 KeyAttributeのみを作成しない –

+0

"データベースからモデルを更新"してもIdAフィールドをKeyAttributeに変更しないで、あなたの効果をお寄せください –

+0

T4を使用してモデルを生成する場合、上記の後半部分。詳細については、このドキュメントを参照してください。http://stackoverflow.com/documentation/entity-framework/4414/database-first-model-generation/27049/adding-data-annotations-to-the-generated-model –

関連する問題