Ein Versuch wurde unternommen, zu Befestigen oder Fügen Sie eine Entität, ist nicht neu, vielleicht geladen wurden, von einem anderen DataContext
Ich hab ein problem mit NotSupportedException, ich bin immer:
"Ein Versuch wurde unternommen, zu Befestigen oder Fügen Sie eine Entität, ist nicht neu, vielleicht geladen wurden, von einem anderen DataContext."
partial class SupplyOfert : Model
{
public SupplyOfert(int id = 0)
{
if (id > 0)
this.get(id);
}
public Supplier supplier
{
get
{
return this.Supplier;
}
set
{
this.Supplier = db.Suppliers.SingleOrDefault(s => s.id == value.id);
}
}
public override bool get(int id)
{
SupplyOfert so = (SupplyOfert)db.SupplyOferts.SingleOrDefault(s => s.id == id).Copy(this, "Supplies");
this._Supplies = so._Supplies;
so._Supplies.Clear();
if (this.id > 0)
{
db.Dispose();
db = new Database();
db.SupplyOferts.Attach(this); //here I'm getting the exception
return true;
}
else
return false;
}
public override void save()
{
if (this.id <= 0)
db.SupplyOferts.InsertOnSubmit(this);
db.SubmitChanges();
}
public override void remove()
{
if (this.id > 0)
{
db.SupplyOferts.DeleteOnSubmit(this);
db.SubmitChanges();
}
}
}
Wenn ich neuen Datensatz hinzufügen mit:
SupplyOfert ofert = new SupplyOfert();
ofert.price = 100;
ofert.publisher = "some publisher";
ofert.supplier = new Supplier(1);
ofert.title = "some title";
ofert.year = DateTime.Today;
ofert.save();
Ist es ok, aber wenn ich will, update record, mit:
SupplyOfert ofert = new SupplyOfert(2);
ofert.price = 220;
ofert.publisher = "new publisher";
ofert.save();
Ich bin immer "NotSupportedExceptionw war nicht behandelte".
Hier ist die Model-Klasse:
public abstract class Model
{
protected Database db = new Database();
public Model(){}
~Model()
{
db.Dispose();
}
abstract public bool get(int id);
abstract public void save();
abstract public void remove();
}
Und hier ist die copy-Methode:
public static class ObjectProperties
{
public static object Copy(this object source, object destination, string skip = "")
{
if (source != null)
{
foreach (var sourceProperty in source.GetType().GetProperties())
{
foreach (var destinationProperty in destination.GetType().GetProperties())
{
if (destinationProperty.Name == sourceProperty.Name && destinationProperty.Name != skip)
destinationProperty.GetSetMethod().Invoke(destination, new object[] { sourceProperty.GetGetMethod().Invoke(source, null) });
}
}
}
else
destination = null;
return source;
}
}
Datenbank-Klasse, es ist nur:
public class Database : DataClasses1DataContext
Habe ich heraus gefunden, dass dieses problem ist verbunden mit der Abtretung:
this.supplier = so.supplier;
Aber ich weiß nicht, warum...
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dem Thema hier aussieht, erstellen Sie einen separaten Kontext, die mit jeder Entität (das Modell hat Sie eine neue Kopie der db instanziiert). So was passiert, ist:
Den ersten Schritt zu korrigieren, um nicht einen neuen Kontext innerhalb der Einheit.
Stimme ich @Matthäus
Ich würde auch gerne vorschlagen, dass dbcontexts sollte in einem using-block:
Dies ist auch eine nette Lektüre für Datenbank-Kontexten