2017-01-26 2 views
0

Entity Framework와 관련된 도움이 필요합니다. 공급 업체가 존재 추가하지 않고 내 상황에 송장을 추가하는 방법 : 내가 여기에 가상 supplier 특성Entity Framework에서 컨텍스트에 추가

public class Invoices 
{ 
    public string InvoiceNumber { get; set; } 
    public string Department { get; set; } 
    public int SupplierId{ get; set; } 

    [ForeignKey("SupplierId")] 
    public virtual Supplier SupplierDetails { get; set; } 
} 

있다

public class Supplier 
{ 
    [Key, Column("SupplierId")] 
    public int Id { get; set; } 
    public bool IsDeleted { get; set; } 
    public string Name { get; set; } 
    [Required] 
    public string Address { get; set; } 
    public string ContactNumber { get; set; } 

    [Required] 
    [EmailAddress] 
    public string EmailAdd { get; set; } 
} 

내 질문 인 supplier 클래스의 구조의 클래스를 가지고 내 데이터베이스?

여기 이해할 수있을 것입니다 추가

public ActionResult AddInvoice(Invoices inv) 
{ 
    context.invoices.add(inv); 
    return View(); 
} 

어떤 도움에 대한 내 코드입니다 - 희망 누군가가 나에게 도움이 될 수 있습니다. 감사합니다

+1

그리고 어떤 문제가 발생합니까? 유일한 문제는'context.SaveChanges()'가 누락 될 것입니다. – Mats391

+2

'SupplierId'가 nullable이 아닙니다. 따라서 '공급 업체'를 설정해야합니다. [이 비슷한 질문] 참조 (http://stackoverflow.com/questions/41826138/insert-first-set-of-data-to-ef-code-first-using-repository-pattern/41826550#41826550) – smoksnes

+0

하지만 난 내 공급자 디렉토리에 존재하지 않는 경우 세부 사항을 추가해야합니다. – Neil

답변

1

SupplierId 속성을 nullable int 유형 (int?)으로 설정하십시오. 이렇게하면 연결된 공급자없이 송장을 삽입 할 수 있습니다.

+0

하지만 내 공급 업체 디렉토리에 세부 정보를 추가해야하는 경우 필요합니다. – Neil

0

이번에는 특정 코드에 도달 할 수 있도록 작은 코드를 추가합니다.

코드 아래 비린내 같은

public ActionResult AddNewAsset(AssetHeaderDetails entity) 
    { 
     if (IsExist<Supplier>(entity.SupplierDetails)) 
     { 
      entity.SupplierId = entity.SupplierDetails.Id; 
      entity.SupplierDetails = null; 
     } 
     AddDataToContext(entity); 
     AddToLookUp("AssetItemDesc", entity.Description,  entity.Description); 
     if(entity.AssetItemDetails != null) 
     { 
      foreach (var item in entity.AssetItemDetails) 
      { 
       AddToLookUp("AssetItemDetail", item.ItemDescription, item.ItemType); 
      } 
     } 
     return RedirectToAction("AddNewAsset"); 
    } 

의 모습을 볼 수 있지만 그것은

더 좋은 코드를 언급하는 모든 사람에 bit..thanks을 관리 할 수 ​​있습니다하십시오? 여기에 의견을 말하면 코드를 변경하려고합니다 .. 큰 감사 .. :

관련 문제