This question is locked. New answers and comments are not allowed.
I have the following code. The inserts work, the updates do not. Please advise. ( I have the project with all source code zipped up and ready to send you, but that doesn't seem possible in this forum.)
static
void
Main(
string
[] args)
{
using
(FluentModel dbContext =
new
FluentModel ())
{
// Inserts do work
_Interface _iNew =
new
_Interface(dbContext);
_iNew.Caption =
"Dave New"
;
_iNew.Save();
}
using
(FluentModel dbContext =
new
FluentModel())
{
// Updates don't work!!!
_Interface _iUpdate =
new
_Interface(dbContext, 1060);
_iUpdate.Caption =
"Dave Update"
;
_iUpdate.Save();
}
}
Here are the supporting classes:
_Interface.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Reflection;
using
System.Text;
using
System.Threading.Tasks;
using
SWMonitorDAL;
using
Telerik.OpenAccess;
namespace
SWMonitorBLL
{
public
class
_Interface : _MonitorBase
{
public
int
ID{
get
;
set
;}
public
int
SWInterfaceID{
get
;
set
;}
public
int
SWNodeID{
get
;
set
;}
public
string
Name{
get
;
set
;}
public
string
TypeName{
get
;
set
;}
public
string
TypeDescription{
get
;
set
;}
public
double
Speed{
get
;
set
;}
public
DateTime? LastChange{
get
;
set
;}
public
string
PhysicalAddress{
get
;
set
;}
public
int
AdminStatus{
get
;
set
;}
public
int
OperStatus{
get
;
set
;}
public
long
BandwidthIn{
get
;
set
;}
public
long
BandwidthOut {
get
;
set
; }
public
string
Caption{
get
;
set
;}
public
string
FullName{
get
;
set
;}
public
Single BPSOut{
get
;
set
;}
public
Single BPSIn{
get
;
set
;}
public
Single PercentUtilOut{
get
;
set
;}
public
Single PercentUtilIn{
get
;
set
;}
public
Single PPSOut{
get
;
set
;}
public
Single PPSIn{
get
;
set
;}
public
int
PacketSizeOut{
get
;
set
;}
public
int
PacketSizeIn{
get
;
set
;}
public
Single UcastPPSOut{
get
;
set
;}
public
Single UcastPPSIn{
get
;
set
;}
public
Single McastPPSOut{
get
;
set
;}
public
Single McastPPSIn{
get
;
set
;}
public
Single DiscardsThisHourOut{
get
;
set
;}
public
Single DiscardsThisHourIn{
get
;
set
;}
public
Single DiscardsTodayOut{
get
;
set
;}
public
Single DiscardsTodayIn{
get
;
set
;}
public
Single ErrorsThisHourOut{
get
;
set
;}
public
Single ErrorsThisHourIn{
get
;
set
;}
public
Single ErrorsTodayOut{
get
;
set
;}
public
Single ErrorsTodayIn{
get
;
set
;}
public
Single MaxInBPSToday{
get
;
set
;}
public
Single MaxOutBPSToday{
get
;
set
;}
public
DateTime? MaxInBPSTime{
get
;
set
;}
public
DateTime? MaxOutBPSTime{
get
;
set
;}
public
DateTime? LastSync{
get
;
set
;}
public
string
Alias{
get
;
set
;}
public
string
MAC{
get
;
set
;}
public
TimePeriod ValidFor {
get
;
set
; }
private
SolarWindsInterface InterfaceDAO;
public
_Interface(FluentModel dbC) {
dbContext = dbC;
}
public
_Interface(FluentModel dbC,
int
interfaceId,
int
swIntId = 0)
{
dbContext = dbC;
fill(interfaceId, swIntId);
}
public
_Interface(FluentModel dbC,_Interface _oldInt)
{
dbContext = dbC;
CopyFrom(_oldInt);
}
public
void
fill(
int
interfaceId,
int
swIntId = 0)
{
SolarWindsInterface intf =
null
;
if
(swIntId != 0)
{
this
.SWInterfaceID = swIntId;
intf = (from swInt
in
dbContext.Scope.Extent<SolarWindsInterface>()
where swInt.SWInterfaceID == swIntId
&& (swInt.ValidFor.StartDateTime <= DateTime.Now && swInt.ValidFor.EndDateTime >= DateTime.Now)
select swInt).FirstOrDefault<SolarWindsInterface>();
}
else
{
this
.ID = interfaceId;
intf = (from intfc
in
dbContext.Scope.Extent<SolarWindsInterface>()
where intfc.ID == interfaceId
select intfc).FirstOrDefault<SolarWindsInterface>();
}
if
(intf ==
null
)
{
this
.Error =
"Interface not found"
;
return
;
}
InterfaceDAO = intf;
this
.AdminStatus = intf.AdminStatus;
this
.Alias = intf.Alias;
this
.BandwidthIn = intf.BandwidthIn;
this
.BandwidthOut = intf.BandwidthOut;
this
.BPSIn = intf.BPSIn;
this
.BPSOut = intf.BPSOut;
this
.Caption = intf.Caption;
this
.DiscardsThisHourIn = intf.DiscardsThisHourIn;
this
.DiscardsThisHourOut = intf.DiscardsThisHourOut;
this
.DiscardsTodayIn = intf.DiscardsTodayIn;
this
.DiscardsTodayOut = intf.DiscardsTodayOut;
this
.ErrorsThisHourIn = intf.ErrorsThisHourIn;
this
.ErrorsThisHourOut = intf.ErrorsThisHourOut;
this
.ErrorsTodayIn = intf.ErrorsTodayIn;
this
.ErrorsTodayOut = intf.ErrorsTodayOut;
this
.FullName = intf.FullName;
this
.ID = intf.ID;
this
.LastChange = intf.LastChange;
this
.LastSync = intf.LastSync;
this
.MAC = intf.MAC;
this
.MaxInBPSTime = intf.MaxInBPSTime;
this
.MaxInBPSToday = intf.MaxInBPSToday;
this
.MaxOutBPSTime = intf.MaxOutBPSTime;
this
.MaxOutBPSToday = intf.MaxOutBPSToday;
this
.McastPPSIn = intf.McastPPSIn;
this
.McastPPSOut = intf.McastPPSOut;
this
.Name = intf.Name;
this
.OperStatus = intf.OperStatus;
this
.PacketSizeIn = intf.PacketSizeIn;
this
.PacketSizeOut = intf.PacketSizeOut;
this
.PercentUtilIn = intf.PercentUtilIn;
this
.PercentUtilOut = intf.PercentUtilOut;
this
.PhysicalAddress = intf.PhysicalAddress;
this
.PPSIn = intf.PPSIn;
this
.PPSOut = intf.PPSOut;
this
.Speed = intf.Speed;
this
.SWInterfaceID = intf.SWInterfaceID;
this
.SWNodeID = intf.SWNodeID;
this
.TypeDescription = intf.TypeDescription;
this
.TypeName = intf.TypeName;
this
.UcastPPSIn = intf.UcastPPSIn;
this
.UcastPPSOut = intf.UcastPPSOut;
this
.ValidFor = intf.ValidFor;
}
public
int
Save()
{
//Scope.Transaction.Begin();
try
{
SolarWindsInterface intf = transform();
if
(intf.ID == 0)
{
dbContext.Add(intf);
dbContext.FlushChanges();
ID = intf.ID;
}
dbContext.SaveChanges();
}
catch
(Exception ex)
{
dbContext.ClearChanges();
}
return
ID;
}
public
SolarWindsInterface transform()
{
SolarWindsInterface intf;
if
(InterfaceDAO !=
null
)
intf = InterfaceDAO;
else
intf =
new
SolarWindsInterface();
intf.AdminStatus = AdminStatus;
intf.Alias = Alias;
intf.BandwidthIn = BandwidthIn;
intf.BandwidthOut = BandwidthOut;
intf.BPSIn = BPSIn;
intf.BPSOut = BPSOut;
intf.Caption = Caption;
intf.DiscardsThisHourIn = DiscardsThisHourIn;
intf.DiscardsThisHourOut = DiscardsThisHourOut;
intf.DiscardsTodayIn = DiscardsTodayIn;
intf.DiscardsTodayOut = DiscardsTodayOut;
intf.ErrorsThisHourIn = ErrorsThisHourIn;
intf.ErrorsThisHourOut = ErrorsThisHourOut;
intf.ErrorsTodayIn = ErrorsTodayIn;
intf.ErrorsTodayOut = ErrorsTodayOut;
intf.FullName = FullName;
intf.ID = ID;
intf.LastChange = LastChange;
intf.LastSync = LastSync;
intf.MAC = MAC;
intf.MaxInBPSTime = MaxInBPSTime;
intf.MaxInBPSToday = MaxInBPSToday;
intf.MaxOutBPSTime = MaxOutBPSTime;
intf.MaxOutBPSToday = MaxOutBPSToday;
intf.McastPPSIn = McastPPSIn;
intf.McastPPSOut = McastPPSOut;
intf.Name = Name;
intf.OperStatus = OperStatus;
intf.PacketSizeIn = PacketSizeIn;
intf.PacketSizeOut = PacketSizeOut;
intf.PercentUtilIn = PercentUtilIn;
intf.PercentUtilOut = PercentUtilOut;
intf.PhysicalAddress = PhysicalAddress;
intf.PPSIn = PPSIn;
intf.PPSOut = PPSOut;
intf.Speed = Speed;
intf.SWInterfaceID = SWInterfaceID;
intf.SWNodeID = SWNodeID;
intf.TypeDescription = TypeDescription;
intf.TypeName = TypeName;
intf.UcastPPSIn = UcastPPSIn;
intf.UcastPPSOut = UcastPPSOut;
if
(ID == 0)
intf.ValidFor =
new
TimePeriod();
else
intf.ValidFor = ValidFor;
return
intf;
}
public
List<_Interface> GetInterfacesBySWNodeID(
int
swNodeID)
{
List<_Interface> _ints =
new
List<_Interface>();
var interfaceIDs = (from i
in
dbContext.Scope.Extent<SolarWindsInterface>()
where i.SWNodeID == swNodeID
&& (i.ValidFor.StartDateTime <= DateTime.Now && i.ValidFor.EndDateTime >= DateTime.Now)
select i.ID).ToList<
int
>();
foreach
(
int
interfaceID
in
interfaceIDs)
{
_Interface _intf =
new
_Interface(dbContext,interfaceID);
_ints.Add(_intf);
}
return
_ints;
}
public
bool
isChanged(_Interface _importCandiateInt)
{
PropertyInfo[] properties =
typeof
(_Interface).GetProperties();
foreach
(PropertyInfo property
in
properties)
{
var currentValue = property.GetValue(_importCandiateInt);
var existingValue = property.GetValue(
this
);
if
(property.Name !=
"ID"
&& property.Name !=
"ValidFor"
&& property.Name !=
"Error"
)
{
if
((currentValue ==
null
&& existingValue !=
null
) || (existingValue ==
null
&& currentValue !=
null
))
{
return
true
;
}
if
(property.PropertyType ==
typeof
(
int
))
{
if
(Convert.ToInt32(currentValue) != Convert.ToInt32(existingValue))
return
true
;
}
else
if
(property.PropertyType ==
typeof
(
string
))
{
if
(currentValue.ToString() != existingValue.ToString())
return
true
;
}
else
if
(property.PropertyType ==
typeof
(DateTime))
{
if
(Convert.ToDateTime(currentValue) != Convert.ToDateTime(existingValue))
return
true
;
}
}
}
return
false
;
}
public
void
ExpireInterfaces(
int
swId)
{
try
{
//Scope.Transaction.Begin();
var intfs = (from swInt
in
dbContext.Scope.Extent<SolarWindsInterface>()
where swInt.SWInterfaceID == swId
&& (swInt.ValidFor.StartDateTime <= DateTime.Now && swInt.ValidFor.EndDateTime >= DateTime.Now)
select swInt).ToList<SolarWindsInterface>();
foreach
(SolarWindsInterface swInt
in
intfs)
{
swInt.ValidFor.EndDateTime = DateTime.Now;
}
dbContext.SaveChanges();
}
catch
(Exception ex)
{
dbContext .ClearChanges();
}
}
private
void
CopyFrom(_Interface _importCandiateInt)
{
PropertyInfo[] properties =
typeof
(_Interface).GetProperties();
foreach
(PropertyInfo property
in
properties)
{
var importedValue = property.GetValue(_importCandiateInt);
if
(property.Name !=
"ID"
)
property.SetValue(
this
, importedValue);
else
property.SetValue(
this
, 0);
}
}
public
_Interface GetActiveIntfBySWIntfID(
int
swId)
{
var intfID = (from i
in
dbContext.Scope.Extent<SolarWindsInterface>()
where i.SWInterfaceID == swId
&& (i.ValidFor.StartDateTime <= DateTime.Now && i.ValidFor.EndDateTime >= DateTime.Now)
select i.ID).FirstOrDefault<
int
>();
return
new
_Interface(dbContext,intfID);
}
}
}
_MonitorBase.cs:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
Telerik.OpenAccess;
namespace
SWMonitorBLL
{
public
abstract
class
_MonitorBase
{
public
string
Error {
get
;
set
; }
protected
FluentModel dbContext;
}
}
Please advise ASAP. Fyi, all projects are enhanced. It would be much easier if I could send you the project and database and you could run it and see for yourself. Without that, I really don't see how we're going to isolate this problem without going back and forth a million times and still getting nowhere.
Thanks,
Dave