无法保存1:1属性:获取“当IDENTITY_INSERT关闭时无法插入”或“从属属性映射到存储生成的列”

时间:2013-05-07 20:27:31

标签: c# .net entity-framework entity-framework-5

我目前正在为正在使用基于Access的20年应用程序以满足其需求的客户创建新应用程序。作为此升级的一部分,他希望我仍然可以转移旧数据记录,以便他的用户仍然可以访问旧数据。

作为大修的一部分,用户现在可以为某些生成的报告定义客户端的默认设置(稍后完成)。这意味着我有两个对象,它们之间的关系为1:1:ClientClientSettings,后者之前没有。我已经继续将旧数据成功导入到我的新系统中,现在我正在测试屏幕和新功能。

尽管如此,我仍然遇到ClientSettings问题。无论我做什么,每当我尝试保存它时,我似乎都会收到错误。这是我目前对ClientSettings所拥有的内容:

public class ClientSettings
{
    [Key, ForeignKey("Client")]
    public int SettingsID { get; set; }

    [Required]
    public FinancialPeriods FinancialDataPeriodDefault { get; set; }

    [Required]
    public FinancialPeriods EmployeeDataPeriodDefault { get; set; }

    [Required]
    public FinancialPeriods MiscDataPeriodDefault { get; set; }

    [Required]
    public bool ExcludeFromComparisonIfNotSubjectAgency { get; set; }

    public virtual Client Client { get; set; }
}

ClientSettings的部分保存功能如下:

var settings = new ClientSettings {
  EmployeeDataPeriodDefault = (FinancialPeriods) View.EmployeeDataComboBox.SelectedIndex,
  FinancialDataPeriodDefault = (FinancialPeriods) View.FinancialDataComboBox.SelectedIndex,
  MiscDataPeriodDefault = (FinancialPeriods) View.MiscDataComboBox.SelectedIndex,
  ExcludeFromComparisonIfNotSubjectAgency = View.ExcludeFromComparisonCheckBox.Checked
 };

 if(SettingsRepository == null) SettingsRepository = new LinkTableRepository<ClientSettings>();

 if (CurrentClient.ClientSettings == null)
 {
   SettingsRepository.Add(settings);
   settings.SettingsID = CurrentClient.ID;
   SettingsRepository.Save();
 }

我要么得到两个例外中的一个。我得到的(当SettingsID设置为DatabaseGeneratedOption.Identity时):

A dependent property in a ReferentialConstraint is mapped to a store-generated column

或者(没有它)我得到:Cannot insert explicit value for identity column in table 'ClientSettings' when IDENTITY_INSERT is set to OFF

如何解决此问题并保存记录?

感谢。

1 个答案:

答案 0 :(得分:0)

看起来EF默认尝试为主键属性创建标识列 尝试明确重置:

[DatabaseGenerated(DatabaseGenerationOption.None)]
[Key, ForeignKey("Client")]
public int SettingsID { get; set; }

(可能,您将不得不删除数据库并再次创建它。)

<强>更新

此代码适用于我:

public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ClientSettings
{
    [Key, ForeignKey("Client"), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SettingsId { get; set; }
    public bool SomeSetting { get; set; }
    public virtual Client Client { get; set; }
}

class MyContext : DbContext
{
    public MyContext()
        : base()
    {
    }

    public DbSet<Client> Clients { get; set; }
    public DbSet<ClientSettings> ClientSettings { get; set; }
}

用法:

            // this is the case, when client and its settings are new entities
            var client = new Client { Name = "Jonh" };
            // note, that in this case you should initialize navigation property
            var clientSettings = new ClientSettings { Client = client, SomeSetting = true };

            context.ClientSettings.Add(clientSettings);
            context.SaveChanges();

            // this is the case, when client already exists in database, and settings are new entity
            var client2 = new Client { Name = "Mary" };
            context.Clients.Add(client2);
            context.SaveChanges();

            // in this case you can initialize either navigation property, or foreign key property
            var clientSettings2 = new ClientSettings { SettingsId = client2.Id, SomeSetting = true };
            context.ClientSettings.Add(clientSettings2);
            context.SaveChanges();

另外,我检查过数据库 - ClientSettings表有非身份主键,这是正确的。