我再次被EF引发的非clrear错误所困扰。我有以下模型类: -
public partial class TMSServer
{
public TMSServer()
{
this.TMSServers1 = new HashSet<TMSServer>();
this.TMSVirtualMachines = new HashSet<TMSVirtualMachine>();
}
public int TMSServerID { get; set; }
public Nullable<int> ServerModelID { get; set; }
public int DataCenterID { get; set; }
public string ILOIP { get; set; }
public int RackID { get; set; }
public Nullable<int> StatusID { get; set; }
public Nullable<int> BackUpStatusID { get; set; }
public int RoleID { get; set; }
public Nullable<int> OperatingSystemID { get; set; }
public Nullable<int> VirtualCenterID { get; set; }
public string Comment { get; set; }
public byte[] timestamp { get; set; }
public long IT360SiteID { get; set; }
public virtual DataCenter DataCenter { get; set; }
public virtual OperatingSystem OperatingSystem { get; set; }
public virtual ServerModel ServerModel { get; set; }
public virtual Technology Technology { get; set; }
public virtual TechnologyBackUpStatu TechnologyBackUpStatu { get; set; }
public virtual TechnologyRole TechnologyRole { get; set; }
public virtual TechnologyStatu TechnologyStatu { get; set; }
public virtual TMSRack TMSRack { get; set; }
public virtual ICollection<TMSServer> TMSServers1 { get; set; }
public virtual TMSServer TMSServer1 { get; set; }
public virtual ICollection<TMSVirtualMachine> TMSVirtualMachines { get; set; }
}
}
以及以下帖子创建动作方法: -
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ServerJoin sj, FormCollection formValues)
{
string controllername = RouteData.Values["controller"].ToString();
if (ModelState.IsValid)
{
//code goes here
repository.InsertOrUpdateServer(sj.Server, User.Identity.Name, assetid);
repository.Save()
以及以下Repository方法: -
public void InsertOrUpdateServer(TMSServer server, string username,long assetid)
{
var resource = GetResourceDetials(assetid);
if (server.TMSServerID == default(int))
{
// New entity
int technologyypeID = GetTechnologyTypeID("Server");
Technology technology = new Technology
{
IsDeleted = true,
TypeID = technologyypeID,
Tag = "S" + GetTagMaximumeNumber(technologyypeID).ToString(),
StartDate = DateTime.Now,
IT360ID = assetid
};
InsertOrUpdateTechnology(technology);
Save();
var auditinfo = IntiateTechnologyAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "ADD").ID,
tms.TechnologyTypes.SingleOrDefault(a => a.Name.ToUpper() == "Server").AssetTypeID,
username, technology.TechnologyID);
server.TMSServerID= technology.TechnologyID;
server.IT360SiteID = resource.SITEID.Value;
tms.TMSServers.Add(server);
technology.IsDeleted = false;
InsertOrUpdateTechnology(technology);
InsertOrUpdateTechnologyAudit(auditinfo);
}
}
但是当我尝试调用Post Create动作方法时,我将得到以下异常: -
System.InvalidOperationException未被用户代码
处理 HResult = -2146233079 Message =违反了Multiplicity约束。该 角色'TMSServers'的关系'TMSModel.FK_Servers_Technology' 具有多重性1或0..1。 Source = System.Data.Entity StackTrace:
异常中包含的Technology模型类如下所示: -
public partial class Technology
{
public Technology()
{
this.TMSSwitchPorts = new HashSet<TMSSwitchPort>();
this.TechnologyAudits = new HashSet<TechnologyAudit>();
this.TechnologyIPs = new HashSet<TechnologyIP>();
}
public int TechnologyID { get; set; }
public string Tag { get; set; }
public bool IsDeleted { get; set; }
public byte[] timestamp { get; set; }
public Nullable<int> TypeID { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<long> IT360ID { get; set; }
public virtual TMSFirewall TMSFirewall { get; set; }
public virtual TMSRack TMSRack { get; set; }
public virtual TMsRouter TMsRouter { get; set; }
public virtual TMSServer TMSServer { get; set; }
public virtual TMSStorageDevice TMSStorageDevice { get; set; }
public virtual TMSSwitch TMSSwitch { get; set; }
public virtual ICollection<TMSSwitchPort> TMSSwitchPorts { get; set; }
public virtual TechnologyType TechnologyType { get; set; }
public virtual ICollection<TechnologyAudit> TechnologyAudits { get; set; }
public virtual ICollection<TechnologyIP> TechnologyIPs { get; set; }
public virtual TMSVirtualMachine TMSVirtualMachine { get; set; }
}