LicenseTypeとEntityTypeの2つのクラスがあります。
[Table("LicenseType")]
public class LicenseType : ComplianceBase, INotifyPropertyChanged
{
private List<Certification> _certifications = new List<Certification>();
private List<EntityType> _entityTypes = new List<EntityType>();
public List<EntityType> EntityTypes
{
get { return _entityTypes; }
set { _entityTypes = value; }
}
public List<Certification> Certifications
{
get { return _certifications; }
set { _certifications = value; }
}
}
と
[Table("EntityType")]
public class EntityType : ComplianceBase, INotifyPropertyChanged
{
private List<LicenseType> _licenseTypes = new List<LicenseType>();
public List<LicenseType> LicenseTypes
{
get { return _licenseTypes; }
set
{
_licenseTypes = value;
// OnPropertyChanged();
}
}
}
どちらもComplianceBaseから派生しています。
public class ComplianceBase
{
private int _id;
private string _name;
private string _description;
public string Description
{
get { return _description; }
set
{
if (_description == value) return;
_description = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public int Id
{
get { return _id; }
set
{
if (value == _id) return;
_id = value;
OnPropertyChanged();
}
}
public string Name
{
get { return _name; }
set
{
if (value == _name) return;
_name = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
私がしたいのは、EntityTypeを1つ以上のLicenseTypeに関連付けることです。たとえば、EntityType "Primary Lender"は、たとえば"LenderLicense"と"MortgageLicense"の2つのLicenseTypeに関連付けることができます。この状況では、EntityTypeテーブルに「PrimaryLender」という1つのレコードを、LicenseTypeテーブルに「LenderLicense」と「MortgageLicense」という2つのレコードが必要です。
関連するLicenseTypesをEntityTypeに追加するためのコードは、次の呼び出しによって実行されます。
_currentEntity.LicenseTypes.Add(licenseType);
そして、呼び出し_context.SaveChanges()
ます;
これらの2つのテーブルを関連付けるルックアップテーブルとして機能する追加のテーブル「EntityTypeLicenseTypes」があります。EntityTypeを2つの関連するLicenseTypeと結合する2つのレコードがあります。
そして、これは機能します。ただし、私のコードはLicenseTypeレコードも追加(複製)し、関連付けられているレコードのLicenseTypeテーブルに追加します。
どうすればこれを防ぐことができますか?