Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新和保存的功能相同吗?_C#_Asp.net Mvc_Web Services - Fatal编程技术网

C# 更新和保存的功能相同吗?

C# 更新和保存的功能相同吗?,c#,asp.net-mvc,web-services,C#,Asp.net Mvc,Web Services,我使用更新函数的代码在这里,它也可以工作 [HttpPost] public bool SaveDefCompny(DefCompanyDTO DefCmpny) { using (RPDBEntities db = new RPDBEntities()) { using (TransactionScope trans = new TransactionScope()) { //the problem is here i

我使用更新函数的代码在这里,它也可以工作

[HttpPost]

public bool SaveDefCompny(DefCompanyDTO DefCmpny)
{

    using (RPDBEntities db = new RPDBEntities())
    {
        using (TransactionScope trans = new TransactionScope())
        {
            //the problem is here incase of saving 

                var UpdateDefCmpnyId = (from CmpnyId in db.DefCompanies
                                        where CmpnyId.Id == DefCmpny.Id
                                        select CmpnyId).First();

                List<DefCompany> list = new List<DefCompany>();
                list.Add(UpdateDefCmpnyId);
                try
                {
                    foreach (DefCompany DefCmpny1 in list)
                    {
                        DefCmpny1.Id = DefCmpny1.Id;

                        DefCmpny1.ShortName = DefCmpny.ShortName;
                        DefCmpny1.FullName = DefCmpny.FullName;
                        DefCmpny1.ContactPerson = DefCmpny.ContactPerson;
                        DefCmpny1.Address1 = DefCmpny.Address1;


                        DefCmpny1.CompanyCity = DefCmpny.CompanyCity;
                        DefCmpny1.CompanyState = DefCmpny.CompanyState;
                        DefCmpny1.CompanyCountry = DefCmpny.CompanyCountry;
                        DefCmpny1.ZipPostCode = DefCmpny.ZipPostCode;
                        DefCmpny1.TelArea = DefCmpny.TelArea;
                        DefCmpny1.CurrentCurrencyCode = DefCmpny.CurrentCurrencyCode;


                        db.SaveChanges();
                        trans.Complete();
                    }
                }
                catch (Exception ex)
                {


                }

            }
            return false;
        }

}

给出null值,因此保存失败,因为记录是新的且不存在于数据库中,所以在保存时如何处理null如何使用try-catch以便在值为null时继续保存代码,添加以下内容如何:

var UpdateDefCmpnyId = (from CmpnyId in db.DefCompanies
                                    where CmpnyId.Id == DefCmpny.Id
                                    select CmpnyId).FirstOrDefault();
if(UpdateDefCmpnyId == null)
{
    //insert
    //(handle the id however you need to for insert. depending on your setup, you might be able to leave it empty and let the database put it in for you)
}
else
{
    //update
    //set the id as you do in the question
}

但当linq查询返回null时,它会停止。如果您注意到我将
.First()
更改为
.FirstOrDefault()
,则不会继续执行?
var UpdateDefCmpnyId = (from CmpnyId in db.DefCompanies
                                    where CmpnyId.Id == DefCmpny.Id
                                    select CmpnyId).FirstOrDefault();
if(UpdateDefCmpnyId == null)
{
    //insert
    //(handle the id however you need to for insert. depending on your setup, you might be able to leave it empty and let the database put it in for you)
}
else
{
    //update
    //set the id as you do in the question
}