从EF 6.0调用存储过程代码首先不插入记录

我是EF的新手,并且正在玩这个。当我试图调用在存储库中创建的存储过程时,我遇到了困难。我运行SQL Server Profiler并且存储过程没有得到执行。任何人都可以请我指出正确的方向吗?从EF 6.0调用存储过程代码首先不插入记录

我使用VS 2012和SQL Server 2008 R2。我在Unit of Work中使用存储库模式,并在服务层调用Uow上的Commit(不确定是否必须按照此方法调用commit,因为文档说该查询的结果从未被数据库跟踪上下文)。

任何帮助将非常感激

事情我必须有和没有在存储过程的名称前面的Exec的关键字到目前为止 1.Tried做

我的仓库代码:

public class CarRentalUserRepository : RepositoryBase<CarRentalUser> 

{

public override void Add(CarRentalUser user)

{

using (var cxt = this.DataContext)

{

var userId = new SqlParameter { ParameterName = "@UserId", Value = 0, Direction = System.Data.ParameterDirection.Output };

var roleId = new SqlParameter("@RoleId", user.RoleId);

var firstName = new SqlParameter("@FirstName", user.FirstName);

var lastName = new SqlParameter("@LastName", user.LastName);

var email = new SqlParameter("@EmailAddress", user.EmailAddress);

var passsword = new SqlParameter("@Password", user.Password);

var active = new SqlParameter("@IsActive", user.IsActive);

//I see for some reason,the connection state is closed.So am opening that connection here(Don't know why)

if (cxt.Database.Connection.State == System.Data.ConnectionState.Closed)

{

cxt.Database.Connection.Open();

}

var result = cxt.Database.SqlQuery<CarRentalUser>(" exec CarRental_InsertUser @UserId, @RoleId, @FirstName, @LastName, @EmailAddress, @Password, @IsActive", userId, roleId, firstName, lastName, email, passsword, active);

if (result != null)

{

}

}

}

public class RepositoryBase<T> where T : class

{

private CarRentalDbContext _dbcontext = null;

private readonly IDbSet<T> dbset;

public RepositoryBase(IDatabaseFactory databseFactory)

{

//DatabaseFactory = databseFactory;

this.DataContext = databseFactory.DbContext;

dbset = DataContext.Set<T>();

}

//protected IDatabaseFactory DatabaseFactory { get;private set; }

protected CarRentalDbContext DataContext

{

get { return _dbcontext; }

set { _dbcontext = value; }

}

public virtual void Add(T entity)

{

dbset.Add(entity);

}

public virtual void Update(T entity)

{

dbset.Attach(entity);

DataContext.Entry(entity).State = EntityState.Modified;

}

public virtual void Delete(T entity)

{

dbset.Remove(entity);

}

public virtual void Delete(Expression<Func<T, bool>> predicate)

{

IEnumerable<T> objects = dbset.Where<T>(predicate).AsEnumerable();

if (objects != null && objects.Any())

{

foreach (T obj in objects)

{

dbset.Remove(obj);

}

}

}

public virtual T GetById(long Id)

{

return dbset.Find(Id);

}

public virtual T GetById(string Id)

{

return dbset.Find(Id);

}

public virtual T Get(Expression<Func<T,bool>> predicate)

{

return dbset.Where(predicate).FirstOrDefault<T>();

}

public virtual IEnumerable<T> GetAll()

{

return dbset.ToList();

}

public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> predicate)

{

return dbset.Where(predicate).AsEnumerable<T>();

}

}

public class UnitOfWork : IUnitOfWork

{

//private readonly IDatabaseFactory databaseFactory;

private CarRentalDbContext _dbContext;

public UnitOfWork(IDatabaseFactory databaseFactory)

{

this.DataContext = databaseFactory.DbContext;

}

public void Commit()

{

if (DataContext != null)

{

DataContext.Commit();

}

}

public CarRentalDbContext DataContext

{

get { return _dbContext; }

set { _dbContext = value; }

}

}

public interface IUnitOfWork

{

void Commit();

}

public class CarRentalDbContext : DbContext

{

public CarRentalDbContext()

: base("CarRental_Connection_String")

{

var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

Database.SetInitializer<CarRentalDbContext>(null);

}

public DbSet<CarRentalUser> Users { get; set; }

public virtual void Commit()

{

base.SaveChanges();

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

modelBuilder.Configurations.Add(new CarRentalUserMap());

modelBuilder.Configurations.Add(new CarRentalRoleMap());

}

}

And my stored procedure

Create PROCEDURE dbo.CarRental_InsertUser (

@UserId INT OUTPUT

,@RoleId INT

,@FirstName VARCHAR(50)

,@LastName VARCHAR(50)

,@EmailAddress VARCHAR(25)

,@Password VARBINARY(500)

,@IsActive BIT

)

AS

BEGIN

INSERT INTO [CarRental].[dbo].[CarRentalUser]

([RoleId]

,[EmailAddress]

,[FirstName]

,[LastName]

,[Password]

,[CreatedDtm]

,[UpdateDtm]

,[IsActive])

VALUES

(

@RoleId,

@EmailAddress,

@FirstName,

@LastName,

@Password,

GETDATE(),

null,

1

)

set @UserId = SCOPE_IDENTITY();

END

回答:

我想我想通了这个问题。我做了以下更改的部分,我让该数据库调用

var result = cxt.Database.SqlQuery<int>("CarRental_InsertUser @UserId,@RoleId,@FirstName,@LastName,@EmailAddress,@Password,@IsActive", 

userId,roleId,firstName,lastName,email,passsword,active);

if (result != null)

{

retVal = result.Single();

}

Siny emy存储过程返回一个int输出参数。我注意到的一件事是,该程序仅在我调用结果后才会执行。单个()

以上是 从EF 6.0调用存储过程代码首先不插入记录 的全部内容, 来源链接: utcz.com/qa/262081.html

回到顶部