EF 4.3.1でentities.savechanges()を呼び出すときにエラーが発生します。私のデータベースはSQL CE v4ストアであり、私はmvvmパターンでコーディングしています。私は観察可能なコレクションに送信する私のコンテキストのローカルバージョンを持っています。これはうまく動作し、データベースに行が存在しないときにsavechanges()を呼び出すとオブジェクトは正常に保持されます。アプリケーションをリロードすると、リストボックスにオブジェクトが表示されますが、別のオブジェクトを追加してsavechanges()を呼び出すと、重複値をユニークインデックスに挿入できないというエラーが表示されます。dbset.localデータベースを更新しています:重複した値を一意のインデックスに挿入できません
私が理解していることは、コンテキストがエンティティをデータストアに保存しようとしていることを意味しますが、元のオブジェクトと新しいオブジェクトを追加しているようです。私は彼らの状態が変わらないので、彼らを一人で残すと思った。
private void Load()
{
entities.Properties.Include("Images").Load();
PropertyList = new ObservableCollection<Property>();
PropertyList = entities.Properties.Local;
//Sort the list (based on previous session stored in database)
var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
PropertyList.Clear();
sortList.ForEach(PropertyList.Add);
propertyView = CollectionViewSource.GetDefaultView(PropertyList);
if (propertyView != null) propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);
private void NewProperty()
{
try
{
if (PropertyList != null)
{
Property p = new Property()
{
ID = Guid.NewGuid(),
AgentName = "Firstname Lastname",
Address = "00 Blank Street",
AuctioneerName = "Firstname Lastname",
SaleTitle = "Insert a sales title",
Price = 0,
NextBid = 0,
CurrentImage = null,
Status = "Auction Pending",
QuadVis = false,
StatVis = false, //Pause button visibility
Sort = PropertyList.Count + 1,
};
PropertyList.Add(p);
SaveProperties();
}
private void SaveProperties()
{
try
{
foreach (var image in entities.Images.Local.ToList())
{
if (image.Property == null)
entities.Images.Remove(image);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
entities.SaveChanges();
}
なぜ私はこれを見ませんでした!新鮮な目を必要とすることについて話し、私がこの午後に戻ったときにそれを撃つでしょう。 – randomalbumtitle