ulf-jzl
asked on
Repository pattern, ADO.Net Entity Framework: Update Method.
HI!
I'm using the "Repository Pattern" and I like to have a Update method.
Need some help to write one.
This is what I have now.
I'm using the "Repository Pattern" and I like to have a Update method.
Need some help to write one.
This is what I have now.
public interface IRepository<T>
where T : class
{
IEnumerable<T> FindAll();
IEnumerable<T> FindBy(Expression<Func<T, bool>> filter);
T FindById(int id);
T FindById(string id);
bool Add(T entity);
bool Update(T entity);
bool Remove(T entity);
}
public class FileSystemWatcherRepository : IRepository<FileSystemWatcher>
{
public IEnumerable<FileSystemWatcher> FindAll()
{
using (var ctx = new FbiDmsEntities())
{
// Return all
return ctx.FileSystemWatcher.ToArray();
}
}
public IEnumerable<FileSystemWatcher> FindBy(Expression<Func<FileSystemWatcher, bool>> filter)
{
using (var ctx = new FbiDmsEntities())
{
// Return item according to predicate
return ctx.FileSystemWatcher.Where(filter);
}
}
public FileSystemWatcher FindById(int id)
{
using (var ctx = new FbiDmsEntities())
{
// Return item by Id
return ctx.FileSystemWatcher.Where(d => d.FileSystemWatcherId == id).FirstOrDefault();
}
}
public FileSystemWatcher FindById(string id)
{
using (var ctx = new FbiDmsEntities())
{
// Return item by folder path id
return ctx.FileSystemWatcher.Where(d => d.FolderPath == id).FirstOrDefault();
}
}
public bool Add(FileSystemWatcher entity)
{
using (var ctx = new FbiDmsEntities())
{
// Add object to entity
ctx.FileSystemWatcher.AddObject(entity);
// Commit
ctx.SaveChanges();
// All ok
return true;
}
}
public bool Update(FileSystemWatcher entity)
{
???
}
public bool Remove(FileSystemWatcher entity)
{
using (var ctx = new FbiDmsEntities())
{
// Delete object from entity
ctx.FileSystemWatcher.DeleteObject(entity);
// Commit
ctx.SaveChanges();
// All ok
return true;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This really should be a separate question with its own points assigned, but...
I'm not sure that I understand the comment that "the store procedure is empty." Stored procedures can't be empty, unless you mean the body of the stored procedure (i.e. it's an empty stored procedure that does nothing).
However, looking at the stored proc your problem would appear to be this line:
WHERE
[FolderPath] = 'C:\Test';
You're hard-coding the folderpath instead of comparing it to the one that's being passed in. So unless the first one you pass in is 'C:\Test' it will keep adding new ones. It should be:
WHERE
[FolderPath] = @FolderPath
I'm not sure that I understand the comment that "the store procedure is empty." Stored procedures can't be empty, unless you mean the body of the stored procedure (i.e. it's an empty stored procedure that does nothing).
However, looking at the stored proc your problem would appear to be this line:
WHERE
[FolderPath] = 'C:\Test';
You're hard-coding the folderpath instead of comparing it to the one that's being passed in. So unless the first one you pass in is 'C:\Test' it will keep adding new ones. It should be:
WHERE
[FolderPath] = @FolderPath
ASKER
I will move this to another question, so you can help me there instead.
Thx for the error, but what I mean is.
The ctx.SaveChanges(); always returns 1. even if I would empty the body of the store procedure.
The error I had did not solve the problem. it still return one even if it did not add any data to the table.
Thx for the error, but what I mean is.
The ctx.SaveChanges(); always returns 1. even if I would empty the body of the store procedure.
The error I had did not solve the problem. it still return one even if it did not add any data to the table.
ASKER
One question more, if you have the time. :)
I have a Store Procedure that executes the INSERT statement, I have done all the mapping and it works fine. The first insert works, but when I run it again it shold return affected rows = zero.
But it always returns 1, even if the store procedure is empty.
If you have any thoughts about the code, please tell me, maybe I have done something strange. :)
Open in new window