質問

I'm having a problem with my asp.net webapi project. I'm using ODP.NET for connecting with an Oracle Database. My webapi is build up out of 3 layers (service, core, dal).

My problem is that my database won't update when i'm trying to change/create/delete an object, but the DBContext in the ef will. So when i'm calling my api through "api/user/1" with a put request the ef updates the object, but doesn't commit it to the database.

The weird part is, when i'm trying to update an object through unittesting the database will update. But then i'm calling the controller directly: UserController.Put(id, object).

Does someone knows how this is possible and how I can solve this?

EDIT: CODE

UnitTest:

var _controller = new UserController();

var user = new User()
{
    Name = "userTest"
};

//Adding user through web -- Doesn't add to database
var _client = new RestClient("http://localhost");

RestRequest request = new RestRequest("api/User", Method.POST);
request.AddParameter("text/json", JsonConvert.SerializeObject(user), ParameterType.RequestBody);

IRestResponse response = _client.Execute(request);

//Adding user through controller -- Does add to database
_controller.Post(user);

var userData = (List<User>)_controller.Get();
Assert.IsNotNull(userData.Find(u => u.Naam == user.Naam));

UserController:

// POST api/User
public void Post([FromBody]T value)
{
    _service.Create(value);
}

UserService:

public void Create(User value)
{
    var _repository = new UserRepository();
    var _mapper = new UserMapper();
    var newUser = _mapper.Map(value);

    _repository.Add(newUser);
    _repository.SaveChanges();
}
役に立ちましたか?

解決

Do you use dependency injection? If so, try to find if there is a difference.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top