내 모델은Insert 문이 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 명세서가 종료되었습니다."
hover model.Name은 값을 반환하지만 나머지는 위의 오류의 원인으로 의심되는 null 값을 반환합니다.
이전에 비슷한 코드를 사용했기 때문에 어떤 문제가있을 수 있습니다. 아무도 도와 줄 수 있습니다. 미리 감사드립니다.
'user' var에 이미 문제가있을 수 있습니까? 나는. 'user.CompanyId'는 값을 가지지 않거나 (올바른 것이 아닌)? –
'뱅킹 '이란 무엇입니까? '엔티티 '란 무엇입니까? 모델이 아니 어야지 .Offices.Add (entry) ?;; – Kaf
예 뱅킹은 은행 정보를 보유하고있는 엔티티입니다. – Muzingaye