我是一个新手学习实体框架(VS2012),正在编写一个简单的 CRUD 应用程序进行测试。我为简单的插入/更新创建了以下函数。我想知道它是否可以或有任何缺陷并且可以改进?
此函数将位于类库类文件中,并将在表单提交时从 Web UI 调用。
这是功能:
public static bool Save(int id, string hospitalname, string hospitaladdress, int cityid,
string postcode, int countryid, string email, string phone, string fax, string contactperson,
string otherdetails, bool isactive, DateTime createddate)
{
bool flag = false;
using (var dataContext = new pacsEntities())
{
if (id == 0)
{
// insert
var newhospital = new hospital_master();
newhospital.hospitalname = hospitalname;
newhospital.hospitaladdress = hospitaladdress;
newhospital.cityid = cityid;
newhospital.postcode = postcode;
newhospital.countryid = countryid;
newhospital.email = email;
newhospital.phone = phone;
newhospital.fax = fax;
newhospital.contactperson = contactperson;
newhospital.otherdetails = otherdetails;
newhospital.isactive = isactive;
newhospital.createddate = DateTime.Now;
dataContext.hospital_master.AddObject(newhospital);
dataContext.SaveChanges();
flag = true;
}
else
{
// update
var hospital = dataContext.hospital_master.First(c => c.hospitalid == id);
if (hospital != null)
{
hospital.hospitalname = hospitalname;
hospital.hospitaladdress = hospitaladdress;
hospital.cityid = cityid;
hospital.postcode = postcode;
hospital.countryid = countryid;
hospital.email = email;
hospital.phone = phone;
hospital.fax = fax;
hospital.contactperson = contactperson;
hospital.otherdetails = otherdetails;
hospital.isactive = isactive;
dataContext.SaveChanges();
flag = true;
}
}
}
return flag;
}