C#实体框架和存储过程

时间:2018-07-20 16:56:48

标签: c# stored-procedures entity-framework-6

我先使用存储过程创建数据库,然后将其附加到我的项目Entity Framework数据库中。

此功能可插入公司信息并返回其ID,并将其插入与公司表相关的另一个表中

public string InsertCompany(company company, out int index)
{
        try
        {
            using (vendors_managerEntities db = new vendors_managerEntities())
            {
                db.companies.Add(company);
                db.SaveChanges();
                index = company.id_company;
                return $"{company.name_company} Is Saved";
            }
        }
        catch (Exception ex)
        {
            index = 0;
            return ex.Message;
        }
}

但是当我尝试在数据库中创建的存储过程时,我无法返回任何值,该ID始终为0

public string InsertCompany(company company, out int index)
{
        try
        {
            using (vendors_managerEntities db = new vendors_managerEntities())
            {
                db.SP_insert_companies(company.name_company, company.website_company, company.adress_company, company.county_company, company.decription_company);
                index = company.id_company;
                return $"{company.name_company} Is Saved";
            }
        }
        catch (Exception ex)
        {
            index = 0;
            return ex.Message;
        }
}

我了解到可以在SQL中使用它,但我正在寻找C#中的解决方案,因此我在C#中打开了存储过程定义,并找到以下代码,并在考虑是否可以更改其返回值,因为它不是返回ID值

public virtual int SP_insert_companies(string name, string website, string address, string country, string description)
{
        var nameParameter = name != null ?
            new ObjectParameter("name", name) :
            new ObjectParameter("name", typeof(string));

        var websiteParameter = website != null ?
            new ObjectParameter("website", website) :
            new ObjectParameter("website", typeof(string));

        var addressParameter = address != null ?
            new ObjectParameter("address", address) :
            new ObjectParameter("address", typeof(string));

        var countryParameter = country != null ?
            new ObjectParameter("country", country) :
            new ObjectParameter("country", typeof(string));

        var descriptionParameter = description != null ?
            new ObjectParameter("description", description) :
            new ObjectParameter("description", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SP_insert_companies", nameParameter, websiteParameter, addressParameter, countryParameter, descriptionParameter);
}

请告诉我C#中是否有解决方案,或者在这种情况下我应该回到没有存储过程的旧代码吗?

2 个答案:

答案 0 :(得分:1)

问题是您正在使用存储过程插入company实体,我怀疑这不会导致上下文刷新对象:

db.SP_insert_companies(company.name_company, company.website_company, company.adress_company, company.county_company, company.decription_company);

然后您尝试从0对象获取ID,因为它尚未刷新:

index = company.id_company;

如果您坚持使用存储过程,那么我建议您让SP返回公司的ID,然后从调用中获取它并将其用作index的值:

index = db.SP_insert_companies(company.name_company, company.website_company, company.adress_company, company.county_company, company.decription_company);

一旦修改了SP,请确保更新代码中的定义,以便它知道要创建一个返回值的函数。

如果您希望将其包含在对象本身中,请确保手动更新它,尽管我不建议这样做,因为该对象与数据库未真正同步:

index = db.SP_insert_companies(company.name_company, company.website_company, company.adress_company, company.county_company, company.decription_company);
company.id_company = index;

答案 1 :(得分:0)

根据您所说的内容,它会自动转到“ Catch”,然后索引已设置为0。因此,您的代码在其他地方(未列出)很可能搞砸了。我怀疑您保存公司信息的代码在任何地方都无法正确保存。尝试将公司手动输入数据库中,然后使用程序进行检查。如果返回的是“已保存”,则说明您的问题不在此方法之内,并且是您保存方法的结果。