2017-04-04 130 views
0

我想使用實體框架6插入和更新表格。我無法做到這一點。無法在實體框架中添加或更新表格6

當我插入,我得到一個錯誤

無法更新EntitySet的「SampleV2」,因爲它有一個DefiningQuery並沒有元素的元素存在,以支持當前的操作

當我更新後,拋出的錯誤是:

屬性'名稱'是對象的關鍵信息的一部分,無法修改。

我的代碼:

//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

嘗試在數據庫和「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文件中設置StoreGeneratedPatternIdentity在DB添加主鍵,然後從數據庫更新生成的模型,以確保映射的實體類具有標識主鍵字段。

注:該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已經設置爲Identity。 KeyAttribute只是不會創建 –

+0

感謝您的效果,做完「從數據庫更新模型」後也不會將Id字段更改爲KeyAttribute –

+0

如果您使用T4的模型生成,我已經給出瞭如何自動添加'KeyAttribute'在EDMX文件中的每個模型更新上面的後半部分。查看此文檔以獲取更多詳細信息:http://stackoverflow.com/documentation/entity-framework/4414/database-first-model-generation/27049/adding-data-annotations-to-the-generated-model。 –