0

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

作为大修的一部分,用户现在可以为一些生成的报告定义客户端的默认设置(稍后完成)。这意味着我有 2 个对象,它们之间存在 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

我该如何解决这个问题并保存记录?

谢谢。

4

1 回答 1

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具有非身份主键,这是正确的。

于 2013-05-07T20:45:04.850 回答