2013-07-22 72 views
0

我碰到一個很奇怪的場景來了,我有這個模型(客戶)我的EDMX內部文件更新表字段使用EF數據庫首先usind EDMX

public int ID { get; set; } 
    public string CompanyName { get; set; } 
    public string PhoneNo { get; set; } 
    public string FaxNo { get; set; } 
    public string CustomerNo { get; set; } 
    public Nullable<System.DateTime> Modified { get; set; } 
    public System.DateTime Created { get; set; } 
    public Nullable<int> PMFormID { get; set; } 
    public Nullable<int> InspectionFormID { get; set; } 
    public Nullable<int> RepairFormID { get; set; } 
    public string Email { get; set; } 

現在,當我嘗試更新此`

Customer customerToUpdate = _context.Customers.Where(c => c.ID == customer.ID).SingleOrDefault(); 
     customerToUpdate.CompanyName = customer.CompanyName; 
     customerToUpdate.CustomerNo = customer.CustomerNo; 
     customerToUpdate.PhoneNo = customer.PhoneNo; 
     customerToUpdate.FaxNo = customer.FaxNo; 
     customerToUpdate.Modified = DateTime.Now; 
     customerToUpdate.InspectionFormID = customer.InspectionFormID; 
     customerToUpdate.PMFormID = customer.PMFormID; 
     customerToUpdate.RepairFormID = customer.RepairFormID; 
     customerToUpdate.Email = customer.Email; 
     context.SaveChanges(); 

除修改字段外,所有字段都已更新。它的Tha值仍然爲空。哪裏不對?請幫忙

+0

'modified'(每個數據庫)的列類型是什麼? –

+0

列類型是DateTime,我對其他實體做了同樣的處理,但它的工作原理令人沮喪,因此在這個實體中它不起作用。也許有些問題我從來不知道。我使用EF數據庫第一種方法使用EDMX(拖放一個)。 – user2457699

+0

它可以在數據庫中爲空嗎? –

回答

0

真奇怪。試着這樣說:

customer.Modified = DateTime.Now; 

// This should find Customer by EntityKey and apply all fields to context entry 
// so you shouldn't assign each property value 
_context.Customers.ApplyCurrentValues(customer); 

_context.SaveChanges(); 

或者可能最好是在數據庫級別爲SQL UPDATE觸發器來實現Modified領域的行爲?

相關問題