Ich versuche, Daten von Entity Framework zu SQL Server zu speichern; das funktioniert normalerweise und ich weiß nicht, was ich hier falsch mache, es wirft überhaupt keinen Fehler auf.Entity Framework speichert keine Daten
try
{
tblPortalCustomerInfo _custInfo = new tblPortalCustomerInfo();
_custInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
_custInfo.addressLine1 = tbaddress1.Text;
_custInfo.addressLine2 = tbaddress2.Text;
_custInfo.town = tbtown.Text;
_custInfo.county = "test";
_custInfo.postcode = tbpostCode.Text;
_custInfo.email = tbEmail.Text;
_custInfo.phone = tbDayPhone.Text;
if(_custInfo.EntityState == System.Data.EntityState.Detached)
{
_da.portalEntities.tblPortalCustomerInfoes.AddObject(_custInfo);
}
_da.SaveChanges();
}
catch (Exception ex)
{
throw;
}
Ich habe Schritt für Schritt durch den Code und kein Fehler erzeugt wird, und es ist an immer ok hier ist meine GetCustById
Funktion
// <returns></returns>
public tblPortalCustomerInfo GetCustById(Guid id)
{
try
{
tblPortalCustomerInfo _customerInfo;
_customerInfo = (from _custInfo in _dal.portalEntities.tblPortalCustomerInfoes
where _custInfo.id == id
select _custInfo).FirstOrDefault();
//If the empNotes entity is null create a new one and attach it to the context.
if (_customerInfo == null)
{
_customerInfo = new tblPortalCustomerInfo();
_dal.portalEntities.tblPortalCustomerInfoes.AddObject(_customerInfo);
}
return _customerInfo;
}
catch (Exception ex)
{
string inner = string.Empty;
if (ex.InnerException != null)
{
inner = ex.InnerException.ToString();
}
return null;
}
}
Mein Portal Einheiten ist nur aus meinem Kontext Basis
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
Bevor Sie sagen, mache ich einen Postback-Check Ja, ich bin hier auf meiner Seite laden
protected void Page_Load(object sender, EventArgs e)
{
/****** Script for SelectTopNRows command from SSMS ******/
/****** Script for SelectTopNRows command from SSMS ******/
tblPortalCustomerInfo _myCustomerInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
if (!IsPostBack)
{
tbaddress1.Text = _myCustomerInfo.addressLine1;
tbaddress2.Text = _myCustomerInfo.addressLine2;
tbtown.Text = _myCustomerInfo.town;
tbpostCode.Text = _myCustomerInfo.postcode;
tbMobile.Text = _myCustomerInfo.mobile;
tbEmail.Text = _myCustomerInfo.email;
tbLandLine.Text = _myCustomerInfo.phone;
}
}
Mein Portal Entitäten Eigenschaft, die in meinem Zusammenhang Basis ist
public portalEntities1 _portalEntities;
public portalEntities1 portalEntities
{
get
{
if (_portalEntities == null)
{
try
{
_portalEntities = new portalEntities1();
}
catch (Exception ex)
{
}
}
return _portalEntities;
}
}
bekomme ich keine Fehler die Informationen einfach nicht an die DB aus irgendeinem Grund jemand gerettet irgendwelche Ideen
Anmerkung 1
Ok, also ich benutze ef5 ist dies die richtige Methode, um einen Objektzustand zu ändern, der mir logisch erschien
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
tblPortalCustomerInfo _custInfo = new tblPortalCustomerInfo();
_custInfo = _da.GetCustById(new Guid("397E4A4B-CD89-43DC-9E70-C19FC27EE6E4"));
_custInfo.addressLine1 = tbaddress1.Text;
_custInfo.addressLine2 = tbaddress2.Text;
_custInfo.town = tbtown.Text;
_custInfo.county = "test";
_custInfo.postcode = tbpostCode.Text;
_custInfo.email = tbEmail.Text;
_custInfo.phone = tbDayPhone.Text;
_da.portalEntities.ObjectStateManager.ChangeObjectState(_custInfo, System.Data.EntityState.Modified); ----> is this correct for ef5
_da.SaveChanges();
}
}
Wenn ich das obige Verfahren versuche ich vorgestellt habe mit
Ausnahme geworfen: ‚System.InvalidOperationException‘ in System.Data.Entity.dll Zusätzliche Informationen: Der Object keine Object mit einem Verweis enthalten ein Objekt vom Typ 'portalef.tblPortalCustomerInfo'.
Es wäre nett, wenn Sie eine Frage mit minimalem Code, der Ihr Problem reproduziert, zur Verfügung stellen. Auch leere 'try'-' catch'-Blöcke sind keine gute Übung. Was Sie hier tun können, gehen Sie zu den Debug-Exceptions und aktivieren Sie das Kontrollkästchen unter ** CLR Exceptions **. Dann wird Ihnen jede ausgelöste Ausnahme im Debug-Modus angezeigt. Sie können auch Schritt-für-Schritt-Protokollierung implementieren, um zu sehen, wo genau es abstürzt. Es ist schwer aus der Seitenperspektive zu sagen, warum dieser Code momentan keine Entitäten speichert. –
@IvanYurchenko Hallo tut mir leid, wo ist diese Option – rogue39nin
Drücken Sie 'Strg + D, E' und dort unter ** CLR-Ausnahmen ** überprüfen Sie beide" geworfen "und" Benutzer nicht behandelt "Optionen. Dies wird Ihnen helfen, geworfene, aber von 'catch' Block-Ausnahmen behandelte Blöcke zu bestimmen. –