简体   繁体   中英

get list from table using stored procedure entity framework

I am using same stored procedure for inserting and fetching data on condition base.

Stored Procedure-

   ALTER PROCEDURE dbo.sp_SaveUserRole
    @company_name nvarchar(50)=null,
    @address nvarchar(250)=null,
    @mobileno int =0,
    @phoneno int=0,
    @faxno int=0,
    @email nvarchar(250)=null,
    @regno int=0,
    @establish_date datetime=null,
    @cond int=0

    AS
    if(@cond=1)
    begin
    insert into Company (company_name,address,mobileno,phoneno,faxno,regno,email,establish_date) values (@company_name,@address,@mobileno,@phoneno,@faxno,@regno,@email,@establish_date)
    end
    else if(@cond=2)
    begin
    select * from company where isactive = 'True';
    end

        RETURN

As on inserting data using entity framework I am doing this-

public ActionResult SaveRole(CompanyModel cmp)
    {
        var company_name = cmp.Company_Name;
        var regno = cmp.regNo;
        var mobileno = cmp.mobileNo;
        var phoneno = cmp.phoneNo;
        var establish_date = cmp.establish_Date;
        var email = cmp.emaiL;
        var address = cmp.Address;
        var faxno = cmp.faxNo;
        db.sp_SaveUserRole(company_name, address, mobileno, phoneno, faxno, email, regno, establish_date,1);
        return Json(new { success = true });

Note- Here condition is 1 so it goes to insert data procedure.

Trying to get a list-

 var list = db.sp_SaveUserRole("", "", 0, 0, 0, "", 0, null, 2).ToList();

I tried this way of getting data from table, where I had to pass necessary arguments to this procedure call. Though I wanted to go this procedure only to 2nd condition I've mentioned there.

So only for this 2nd condition How can I modify this procedure without passing arguments?

Instead of using a stored procedure I would add your company table as an entity in your edmx and access it in code.

That way instead of passing @Cont=2 to a stored proc you can instead use LINQ to access the data you require as the SQL seems very basic.

You can then remove that piece of SQL from your stored proc as mixing Inserts with selects doesnt feel right.

eg

// For insert
if (cont == 1)
{
   // Call your stored proc here

   // or if you add the company table to EF you can use the Insert use the Add 
   // e.g. Rough Example
   _yourEntities.Companies.Add(cmp);

   // Update the DB
   _yourEntities.SaveChanges();
}
else
{
   var companies = _yourEntities.Companies.Where(c => c.isactive == 'True');
}

If this solution is not an option i would still look to split the stored procs into two to make life easily.

Ideally though as you using Entity Framework and are looking to insert and select from a single table you get this functionality for free when you add the Company table as a entity in EF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM