2013-07-22 73 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领域的行为?

相关问题