0

現在、ニーズに合わせて 20 年前の Access ベースのアプリケーションを使用しているクライアントのために、新しいアプリケーションを作成中です。このアップグレードの一環として、ユーザーが引き続き古いデータにアクセスできるように、レガシー データ レコードを引き続き転送することを彼は望んでいます。

オーバーホールの一環として、ユーザーは生成されたレポートのクライアントのデフォルト設定を定義できるようになりました (後で行う必要があります)。これは、1 対 1 の関係を持つ 2 つのオブジェクトがあることを意味します: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();
 }

私は2つの例外のうちの1つを受け取ります。どちらかを取得します (が に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 は、デフォルトで主キー プロパティの ID 列を作成しようとするようです。
これを明示的にリセットしてみてください:

[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 に答える