2013-12-12 129 views
4

我的型號如下...插入語句與FOREIGN KEY約束衝突。實體框架

public class Company 
{ 


    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 

    [Required] 
    [MaxLength(50)] 
    public string Name { get; set; } 

    [Required] 
    [MaxLength(255)] 
    public string Fullname { get; set; } 

    public bool HasFuneralInsuranceParlours { get; set; } 
    public bool HasFuneralInsurancePolicies { get; set; } 
    public bool HasLifeInsurancePolicies { get; set; } 


    public bool IsDeleted { get; set; } 

    public virtual List<Office> Offices { get; set; } 
} 


public class Office 
{ 

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public Guid Id { get; set; } 

    [MaxLength(50)] 
    public string Name { get; set; } 

    [MaxLength(100)] 
    public string Address1 { get; set; } 

    [MaxLength(100)] 
    public string Address2 { get; set; } 

    [MaxLength(100)] 
    public string Address3 { get; set; } 

    [MaxLength(20)] 
    public string Telephone { get; set; } 

    [MaxLength(20)] 
    public string Fax { get; set; } 

    [MaxLength(255)] 
    public string Email { get; set; } 

    public bool IsDeleted { get; set; } 

    public Guid CompanyId { get; set; } 
    public virtual Company Companies { get; set; } 

    public virtual List<Employee> Employees { get; set; } 

} 

和控制器

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(OfficeModel model) 
    { 

     bool success = false; 
     string message = ""; 
     byte[] logo = null; 

     var user = SecurityHelper.GetAuthenticatedUser(this.HttpContext); 
     try 
     { 

      if (ModelState.IsValid) 
      { 
       if (model.Name.IsNullOrWhitespace()) throw new Exception("Unable to create this Employee Name. The Type cannot be blank or spaces."); 
       if (models.Offices.Any(x => x.Name.ToLower() == model.Name.ToLower())) throw new Exception(string.Format("This Office's Name '{0}' already exists. Please check your data.", model.Name.ToUpperCase())); 


       var entry = new Office 
       { 
        Id = Guid.NewGuid(), 
        Name = model.Name.ToUpperCase(), 
        Address1 = model.Address1.ToUpperCase(), 
        Address2 = model.Address2.ToUpperCase(), 
        Address3 = model.Address3.ToUpperCase(), 
        Telephone = model.Telephone.ToUpperCase(), 
        Fax = model.Fax.ToUpperCase(), 
        Email = model.Email.ToUpperCase(), 
        IsDeleted = false, 
        CompanyId = user.CompanyId, 

        Bankings = new List<Banking>() 
        { 
         new Banking 
         { 
          Bank = model.OfficeBank.ToUpperCase(), 
          Account = model.BankAccount.ToUpperCase(), 
          Branch = model.Branch.ToUpperCase(), 
          BranchNo = model.BranchNo.ToUpperCase(), 
          AccountType = model.AccountType.ToUpperCase() 
         } 
        } 
       }; 
       models.Offices.Add(entity); 
       success = true; 
       return RedirectToAction("Index"); 
      } 
      else 
      { 
       message = "An error was cought please check your data and retry"; 
      } 
     } 
     catch (Exception ex) 
     { 
      message = ex.Message; 
     } 

     return View(model); 
    } 

當升調試上述碼L返回以下錯誤

「INSERT語句衝突與FOREIGN KEY約束 \「FK_dbo.Offices_dbo.Companies_CompanyId \」。衝突發生在 數據庫\「PolicyMan ager \「,表\」dbo.Companies \「,列 'Id'。\ r \ n聲明已被終止。」

當我懸停model.Name l我返回一個值,但其餘返回我一個空值,我懷疑這樣的上述錯誤的原因。

有什麼問題可能是因爲我以前使用過類似的代碼,它的工作原理。任何人都可以幫忙預先感謝您

+0

難道是''用戶'var已經有問題嗎?即'user.CompanyId'沒有價值(或不是正確的)? –

+0

什麼是Bankings?什麼是「實體」?它不應該是模型。辦公室。添加(入境)? – Kaf

+0

是銀行業務是持有銀行業務細節的實體.. – Muzingaye

回答

4

您正在添加一個新辦公室。

錯誤說有一個參照完整性問題帶有公司表的外鍵約束。

當你創建你添加以下公司關鍵的順序:

CompanyId = user.CompanyId 

所以看來user.CompanyId是不是被註冊對現有公司的ID。

相關問題