无法在Entity Framework 6中添加或更新表

时间:2017-04-04 10:16:25

标签: asp.net-mvc c#-4.0 entity-framework-6

我正在尝试使用Entity Framework 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;
}

1 个答案:

答案 0 :(得分:0)

如果您使用Database First,则错误表示SampleV2实体或表缺少主键字段或未在模型类中设置。

尝试在KeyAttribute实体上添加id,将其标记为主键字段:

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的表设计器在DB中添加主键,并在EDMX文件中将StoreGeneratedPattern设置为Identity,然后从数据库更新生成的模型以确保映射的实体类有身份主键字段。

注意:可以通过编辑T4模板(KeyAttribute)文件自动生成.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?