Model binding issue in LINQ to SQL - linq-to-sql

I am just now starting to work with LINQ, and am pretty familiar with MVC. I have a strongly typed view that is updating a record. I have successfully done a creation:
This works fine, and creates a record in the database:
public ActionResult Create(TABLEMODEL tableModel)
{
DBDataContext db = new DBDataContext();
if (ModelState.IsValid)
{
db.TABLEMODEL.InsertOnSubmit(tableModel);
db.SubmitChanges();
}
}
But when trying to update:
public ActionResult Manage(TABLEMODEL tableModel)
{
DBDataContext db = new DBDataContext();
if (ModelState.IsValid)
{
db.SubmitChanges();
}
}
This fails, in the sense that it does not update the record in the database. No actual error/exception occurs, and I can step through it just fine.
I am sure I am missing something, but cannot find what. I appreciate any help on this matter.
UPDATE
I did notice that if I get a record using the DataContext:
DBDataContext db = new DBDataContext();
var m = db.TABLEMODELs.Single(m => m.ID == 1);
m.Name = "UpdatedName";
db.SubmitChanges();
This does update, so I assume I am somehow not binding from my model to the LINQ context.
My Solution
I found that you need to retrieve the object and then update that with the form. Simple enough.
[HttpPost]
public ActionResult Manage(int ID, FormCollection form)
{
DBSDataContext db = new DBSDataContext();
var t= db.TABLEMODELs.Single(b => b.ID == ID);
UpdateModel(t);
if (ModelState.IsValid)
{
db.SubmitChanges();
}
return View(t);
}

You should re-query the original tableModel, map the updated row and then update.
Perhaps something like this (example only, not knowing anything about your schema):
var originalTableModel = db.GetById( tableModel.Id);
originalTableModel.FirstName = tableModel.FirstName;
db.SubmitChanges();

Related

EF6 - There is already an open DataReader associated with this Command which must be closed first

I am coding a MVC5 internet application and am using EF6.
I have an Edit ActionResult that is called when an Asset object is edited. I also need to update other objects values when an Asset object is edited. The UpdateAssociatedAssetObjects function does this.
I am getting the following error:
There is already an open DataReader associated with this Command which must be closed first.
In the UpdateAssociatedAssetObjects function, at the following line of code:
if (item.mapMarker.Id == asset.Id)
Here is the Edit ActionResult:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(AssetViewModel assetViewModel)
{
if (ModelState.IsValid)
{
db.Entry(assetViewModel.asset).State = EntityState.Modified;
assetViewModel.asset.lastUpdate = DateTime.Now;
if (assetViewModel.asset.linkFromExternalResource)
{
assetViewModel.asset.webAddress = assetViewModel.webAddress;
}
else
{
assetViewModel.asset.webAddress = assetViewModel.filename;
}
db.Entry(assetViewModel.asset).Property(uco => uco.creationDate).IsModified = false;
db.Entry(assetViewModel.asset).Property(uco => uco.userName).IsModified = false;
assetService.UpdateAssociatedAssetObjects(db, assetViewModel.asset);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(assetViewModel);
}
Here is the UpdateAssociatedAssetObjects function:
public void UpdateAssociatedAssetObjects(CanFindLocationDatabaseContext db, Asset asset)
{
foreach (var item in db.mapLocations)
{
if (item.mapMarker.Id == asset.Id)
{
item.lastUpdate = DateTime.Now;
}
}
}
Can I please have some help with this code?
I have tried placing the UpdateAssociatedAssetObjects function after the await db.SaveChangesAsync() and using a new database context object, but the error still occurs.
Thanks in advance
In your controller method you are already opening a db connection to an entry of asset.
In your method UpdateAssociatedAssetObjects you're trying to open a second db connection reading, while you're main is still open. Get the first object or get the list in the second object.
An alternate solution is to update the db twice.

Passing multiple Include statements into a repository?

I am trying to figure out a way to pass a collection of include statements into my repository so that I can have it include specific entities. Below is some sample code from my repository.
public TEntity GetById(Guid id)
{
return id != Guid.Empty ? GetSet().Find(id) : null;
}
private IDbSet<TEntity> GetSet()
{
return _unitOfWork.CreateSet<TEntity>();
}
The GetByID method calls the GetSet to return the entity set. I was thinking, if I could somehow pass in a collection of entities to include (via an expression) as part of my GetById, this way I wouldn't have to expose the GetSet to my services. So, something like this:
var entity = _repository.GetById(theId, e => {e.Prop1, e.Prop2, e.Prop3});
I could then pass that expression into my GetSet method and pass it into an include statement. Thoughts?
I have done something like this in my code recently. Would the following work for you?
public TEntity GetById(Guid id, params Expression<Func<TEntity, object>>[] includeProperties)
{
if (id == Guid.Empty) return null;
var set = _unitOfWork.CreateSet<TEntity>();
foreach(var includeProperty in includeProperties)
{
set.Include(includeProperty);
}
return set.First(i => i.Id == id);
}
Then you would call it like this...
var entity = _repository.GetById(theId, e => e.Prop1, e=> e.Prop2, e=> e.Prop3);
I know this doesn't exactly follow your pattern, but I think you could refactor it as required.
I don't think Paige Cook's code will work quite as shown.
I've included a modified version of the code that should work instead:
public TEntity GetById(Guid id, params Expression<Func<TEntity, object>>[] includeProperties)
{
if (id == Guid.Empty) return null;
IQueryable<TEntity> set = _unitOfWork.CreateSet<TEntity>();
foreach(var includeProperty in includeProperties)
{
set = set.Include(includeProperty);
}
return set.First(i => i.Id == id);
}
I only spotted this by tracing the SQL generated by Entity Framework, and realised the original code was only giving the illusion of working, by using lazy-loading to populate the entities specified for inclusion.
There's actually a more terse syntax for applying the Include statements using the LINQ Aggregate method, which is in the blog post linked to. My post also improves the method slightly by having a fall-back to the Find method, when no includes are needed and also shows an example of how to implement a "GetAll" method, using similar syntax.
It's bad idea to store context in non-local space, for many reasons.
I modify Steve's code and get this for my ASP.NET MVC projects:
public aspnet_User FirstElement(Func<aspnet_User, bool> predicate = null, params Expression<Func<aspnet_User, object>>[] includes)
{
aspnet_User result;
using (var context = new DataContext())
{
try
{
var set = context.Users.AsQueryable();
for (int i = 0; i < includes.Count(); i++ )
set = set.Include(includes[i]);
if (predicate != null)
result = set.ToList().FirstOrDefault(predicate);
else
result = set.ToList().FirstOrDefault();
}
catch
{
result = null;
}
}
return result;
}
The include method can be strung together in your linq query like so:
var result = (from i in dbContext.TableName.Include("RelationProperty")
.Include("RelationProperty")
.Include("RelationProperty")
select i);

ASP.NET MVC LINQ-2-SQL as model - how to update?

Is it possible to use LINQ2SQL as MVC model and bind? - Since L2S "attachement" problems are really showstopping.
[HttpPost]
public ActionResult Save(ItemCart edCart)
{
using (DataContext DB = new DataContext())
{
DB.Carts.Attach(edCart);
DB.Carts.Context.Refresh(RefreshMode.KeepChanges, edCart);
DB.Carts.Context.SubmitChanges();
DB.SubmitChanges();
}
return RedirectToAction("Index");
}
That does not work. :S
What does your Save View look like?
You can't just attach a new item to the EntitySet like that. -> Attaching requires a lot of checks and it is a real pain to implement. I tried it myself and didn't like it at all.
In your [HttpPost] method you'll need to update the model before you can save it:
[HttpPost]
public ActionResult Save(int id, ItemCart edCart) {
DataContext DB = new DataContext(); // I'm doing this without a using keyword for cleanliness
var originalCart = DB.Carts.SingleOrDefault(c => c.ID == id); // First you need to get the old database entry
if (ModelState.IsValid & TryUpdateModel(edCart, "Cart")) { // This is where the magic happens.
// Save New Instance
DB.SubmitChanges.
return RedirectToAction("Details", new { id = originalCart.ID });
} else {
// Invalid - redisplay with errors
return View(edCart);
}
}
It tries to update the model from the controllers valueprovider using they "Cart" prefix.

In LINQ to SQL based Repositories, should we create a new DataContext within each method?

for example:
class repository {
private DataContext db = new DataContext();
public IQueryable<Blah> someMethod(int id){
return from b in db.Blah ... select b;
}
public IQueryable<Blah> someMethod2(int id){
return from b in db.Blah ... select b;
}
public IQueryable<Blah> someMethod3(int id){
return from b in db.Blah ... select b;
}
}
OR
Should we make a new DataContext WITHIN each of those methods?
I think we are having some errors once user load increases due to us only having ONE DataContext per Repository instance, is this an accurate assumption?
See also the answer to this question.
In short, especially if you are using a repository pattern, you should create and dispose of a datacontext for each unit of work. Typically I use something like:
public someclass someMethod(int id)
{
using (var db = new SomeDataContext())
{
return db.FindMyClass(id);
}
}
What I did personally, is make the repository disposable. You then get constructs like:
void DeleteCustomer(int id)
{
using(var repos = GetRepos())
{
var customer = repos.GetAll<Customer>().Single(x => x.Id == id);
repos.Delete(customer);
repos.SaveChanges();
}
}
This can be implemented by creating the context in the repository ctor, and disposing it in the Dispose() implementation.
You need to make sure you're not adding/changing/delete objects and selecting from the same context. A context is made to 'last' a unit of work.
You need to be careful of stuff like this though:
IQueryable<Customer> GetCustomers()
{
using(var repos = GetRepos())
{
return repos.GetAll<Customer>();
}
}
void Test()
{
// This will throw an exception, because it extends the linq query
// while the context is disposed.
var customers = GetCustomers().Where(x => x.Id == 123);
}
In that case it's better to move the repository outside as much as possible:
IQueryable<Customer> GetCustomers(MyRepository repos)
{
return repos.GetAll<Customer>();
}
void Test()
{
using(var repos = ...)
{
var customers = GetCustomers(repos).Where(x => x.Id == 123);
}
}
I know this is not quite the same but, in a heavily used asp.net site using old fashioned data adapters, I used to open the connection to the database on page init and close it on page prerender. However I found that when the site was under more load the pages began to crawl. I read somewhere that it is always best to open as late as possible and close as early as possible.
Now I open the context in each method, however due to linq 2 sql and its deferred execution I am not sure if this makes much difference.
I would run the SQL profiler during busy moments to see where the bottle neck lies...

Duplicate entries

I have created a universalrepository that takes the type passed to it and when I create data entry method, the entity is created fine, but when I create a linked entity to it, i get the base entity created again. Any ideas why?
Details..
I have divided a specification into multiple tables to manage stuff...
Now I have got a person entity, an applicant entity...(in reality applicant and person are the same), a contractor entity. A contractor can only be created by an applicant and therefore an applicant will always be created and therefore a person will always be created.
When I go on creating a person, it creates a person fine, but when I create an applicant it creates a person again. Likewise when I create a contractor it creates a person and multiple applicants for some reason.
Here is my LINQ to SQL. If you notice in anyway I can improve this code, I will appreciate that too.
here is the repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
namespace ParkingPermit.Models
{
public class UniversalManagerRepository<T> :IRepositoryT<T>
where T:class
{
private Table<T> _table;
private readonly DB _db ;//= new DB();
public UniversalManagerRepository()
{
_db = new DB();
_table = _db.GetTable<T>();
}
#region IRepositoryT<T> Members
public T Create(T create)
{
// _table = new DB().GetTable<T>();
//_db.GetTable(typeof(T)).InsertOnSubmit(create);
_table.InsertOnSubmit(create);
Save();
return create;
}
public void Delete(T delete)
{
throw new NotImplementedException();
}
public T Edit(T edit)
{
throw new NotImplementedException();
}
public T GetItem(int id)
{
throw new NotImplementedException();
}
public T Update(T update)
{
throw new NotImplementedException();
}
public IEnumerable<T> List()
{
//IQueryable i = _db.GetTable(typeof(T)).AsQueryable() ;
return _db.GetTable(typeof(T)) as IEnumerable<T>;
//throw new NotImplementedException();
}
public void Save()
{
//_db.SubmitChanges();
_table.Context.SubmitChanges();
//throw new NotImplementedException();
}
#endregion
}
}
I can post an image of the linq to sql designer if that helps, but I cant see the feature here...
Many thanksalt text http://img509.imageshack.us/img509/2072/linq.jpg
the thing is that when applicant is added and an applicant.Person is assigned from the session(in model binder), it creates a new person, which is actually the original person created in the beginning. How can I avoid that.
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var personType = (Person)controllerContext.HttpContext.Session[PersonSessionKey];
controllerContext.HttpContext.Session[CurrentApplicantSessionKey] = null;
var av = new ApplicantValidator(new ModelStateWrapper(bindingContext.ModelState));
var newApplicant = bindingContext.Model as Applicant;
if (personType == null)
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
"Cannot Update this Instance directly, please restart the application");
// controllerContext.HttpContext.Session[PersonSessionKey] = personType;
}
else if (newApplicant != null)
{
if (newApplicant.Person != null)
{
if (newApplicant.Person.Equals(personType as Person))
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName,
"A person with these details already exists, please restart the application...");
//return
controllerContext.HttpContext.Session[PersonSessionKey] = null;
personType = null;
}
}
else if (av.Validate(newApplicant))
{
if (newApplicant.Person == null)
{
newApplicant.Person = personType as Person;
newApplicant.PersonId = personType.PersonId;
}
}
}
}
I have resolved this part and apparently its now giving issued with update, can anbody find anything unusual.
Answer to my first problem, was that in Model Binders the entity is being manipulated from sessions and the created back to the service layer.
Apparently it seems that because its all happening outside linq orm framework, this entity needs to be recreated as "From clause ...from ..in db." and then linq correctly recognizes it and does the correct job of insertion.
Can anyone help me with the update/edit..please