Windows Phoneのローカルデータベースでちゃんとリレーションとか組んだことがなかったのでテストもかねてDBを活用したアプリを作成してみよう。
と組み始めたらさっそくエラー
An attempt was made to remove a relationship between a {table name a} and a {table name b}. However, one of the relationship’s foreign keys (table b’s foreign key) cannot be set to null.
nullがセットできないと言っている気がする。確かに、該当keyは以下のようにnullは許容していない。
/// <summary>
/// アイテムID
/// </summary>
private int _itemId;
[Column(DbType = "INT NOT NULL")]
public int itemId
{
get
{
return this._itemId;
}
set
{
if (this._itemId != value)
{
this.NotifyPropertyChanging("itemId");
this._itemId = value;
this.NotifyPropertyChanged("itemId");
}
}
}
ためしにnull許容型に変更してみる。
/// <summary>
/// アイテムID
/// </summary>
private int? _itemId;
[Column(DbType = "INT NOT NULL")]
public int? itemId
{
get
{
return this._itemId;
}
set
{
if (this._itemId != value)
{
this.NotifyPropertyChanging("itemId");
this._itemId = value;
this.NotifyPropertyChanged("itemId");
}
}
}
これでエラーは消えたが、腑に落ちない。
さらに、おためしにカラムにCanBeNull=falseをつけてみる。
/// <summary>
/// アイテムID
/// </summary>
private int? _itemId;
[Column(DbType = "INT NOT NULL", CanBeNull=false)]
public int? itemId
{
get
{
return this._itemId;
}
set
{
if (this._itemId != value)
{
this.NotifyPropertyChanging("itemId");
this._itemId = value;
this.NotifyPropertyChanged("itemId");
}
}
}
これだとやはりエラーになった。
ネットで調べるとDeleteOnNull=trueをつけてやればよいらしい。
(参考)http://www.joe-stevens.com/2009/09/18/linq-to-sql-using-entityset-remove-to-delete-records/
Assosiationの指定を以下のように書き換える。
private EntitySet<VisitHistoryTable> _visitHistories = new EntitySet<VisitHistoryTable>();
[Association(ThisKey = "id", OtherKey = "itemId", DeleteOnNull=true)]
public EntitySet<VisitHistoryTable> visitHistories
{
get
{
return this._visitHistories;
}
set
{
this._visitHistories.Assign(value);
}
}
すると・・・
Invalid DeleteOnNull specification for member ‘ItemTable:System.Data.Linq.EntitySet`{table b name} . DeleteOnNull can only be true for singleton association members mapped to non-nullable foreign key columns.
1対1の場合に使うのかな? 今回は1対多なので該当しないのか。
ちゃんと理解できてないのだけど、null許容型に変更した状態で作業を進める。
解決できたら解決編とローカルデータベースの使い方はちゃんと紹介したいなぁ(今後にご期待!!)
Please give us your valuable comment