First of all: I found this post, but I don't fully understand it, so please don't lock it as a duplicate.
I'm trying to do the Edit operation with a ViewModel.
My problem is for some reason adds a new row to the table instead of editing it. It all used to work before I went for the testing.
I believe it's something silly I missed but I have no idea what I'm doing wrong.
I'm using code-first if it makes any difference
My ViewModel:
public class CreateViewModel
{
public string Title { get; set; }
[Display(Name = "Author")]
public int AuthorId { get; set; }
public DateTime? PublicationDate { get; set; }
public float? Edition { get; set; }
public SelectList Authors { get; set; }
}
My Controller functions:
// GET: Books/Edit/5
public ViewResult Edit(int? id)
{
if (id == null)
{
return View("Error");
}
Book book = db.Books.FirstOrDefault(a => a.Id == id);
var vm = new CreateViewModel()
{
AuthorId = book.AuthorId,
Authors = new SelectList(db.Authors, "Id", "Name"),
PublicationDate = book.PublicationDate,
Title = book.Title,
Edition = book.Edition
};
if (book == null)
{
return View("Error");
}
return View(vm);
}
// POST: Books/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CreateViewModel vm)
{
if (ModelState.IsValid)
{
var repo = new EFLibraryRepository();
repo.Save(new Book(){
AuthorId = vm.AuthorId,
PublicationDate = vm.PublicationDate,
Title = vm.Title,
Edition = vm.Edition
});
return RedirectToAction("Index");
}
return View("Edit", vm);
}
My mock repository:
public class EFLibraryRepository : ILibraryRepository
{
AuthorAndBookDbModel db = new AuthorAndBookDbModel();
public IQueryable<Author> Authors { get { return db.Authors; } }
public IQueryable<Book> Books { get { return db.Books; } }
public void Delete(Book book)
{
db.Books.Remove(book);
db.SaveChanges();
}
public void Delete(Author author)
{
db.Authors.Remove(author);
db.SaveChanges();
}
public Book Save(Book book)
{
if (book.Id == 0)
{
db.Books.Add(book);
}
else
{
db.Entry(book).State = System.Data.Entity.EntityState.Modified;
}
db.SaveChanges();
return book;
}
public Author Save(Author author)
{
if (author.Id == 0)
{
db.Authors.Add(author);
}
else
{
db.Entry(author).State = System.Data.Entity.EntityState.Modified;
}
db.SaveChanges();
return author;
}
}
The ILibraryRepository:
public interface ILibraryRepository
{
IQueryable<Book> Books { get; }
IQueryable<Author> Authors { get; }
Book Save(Book book);
Author Save(Author author);
void Delete(Book book);
void Delete(Author author);
}
In your POST method, you never set the value of the Book's Id property so its always 0 (the default value for int) so in turn, you always execute the code to add a new Book.
You first need to include a property in your view model for the id so that its value will be bound in the POST method.
public class CreateViewModel
{
public int? Id { get; set; } // add this
....
Note that you do not need to include a hidden input for it in the view assuming your using the default routes (its value will be bound from the route value in the forms action attribute).
Then in the POST method, set the Id of the Book based on the view model
repo.Save(new Book() {
Id = vm.Id, // add
AuthorId = vm.AuthorId,
....
However, the correct approach when editing existing records is to get the original data model from the repository based on the Id and update its properties, for example
Book book = db.Books.FirstOrDefault(a => a.Id == vm.Id);
book.AuthorId = vm.AuthorId;
....
repo.Save(book);
rather than creating a new Book instance. Some of the benefits of this approach include
Your data models will often include properties that should not be in
the view (for example, properties to indicate the date a records was
created, and by who). Creating a new instance of the data model and
saving it means those properties would be overwritten and set to
their default values.
You can do concurrency checks, for example you can check the
TIMESTAMP values and if they are different, you know that another
user has modified the record in the meantime (and you might take a
different course of action rather than just overwriting the previous
uses changes)
Related
I'm attempting to create an Audit Log for my MVC, Entity Framework website project. I've been able to subscribe to SaveChanges() in my DBContext (and save to my database through another DBContext but same database).
My two questions in the end are:
What does if (!entry.IsRelationship) do exactly? I have a ViewModel that calculates this as True when Saving and another as False. I would expect this to move into the rest of my method to save in the Audit Log.
How can I get the full Namespace of my Object being modified? I was using this: entry.Entity.ToString() but doesn't seem to work when Saving/Editing from a View Model (details below)
Here is a basic setup that I have thus far (Album object/controller works, but AlbumView doesn't):
Ablum class:
public class Album : BaseObject //BaseObject has a few properties, one is Oid (Guid)
{
public string Name { get; set; }
[Column(TypeName = "varchar(MAX)")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Display(Name="Genres")]
public virtual ICollection<AlbumsGenres> AlbumGenres { get; set; }
[Display(Name="Artists")]
public virtual ICollection<AlbumsArtists> AlbumArtists { get; set; }
}
AblumView class:
public class AlbumView
{
[ScaffoldColumn(false)]
public Guid Oid { get; set; }
[Required]
public string Name { get; set; }
[Column(TypeName = "varchar(MAX)")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Display(Name = "Genres")]
public virtual List<AlbumsGenres> AlbumGenres { get; set; }
[Display(Name = "Artists")]
public virtual List<AlbumsArtists> AlbumArtists { get; set; }
}
AlbumsController (Audit works with something like this):
public ActionResult Edit(Album album)
{
if (ModelState.IsValid)
{
db.Entry(album).State = EntityState.Modified;
db.SaveChanges(); //This is where SaveChanges() takes over (see below)
return RedirectToAction("Index");
}
return View(album);
}
AlbumsViewController:
public ActionResult Edit(Guid id, AlbumView albumViewModel)
{
//Omitting setup...
//Album gets updated
Album album = db.Albums.Find(id);
album.Name = albumViewModel.Name;
album.Description = albumViewModel.Description;
//Other Objects are also updated, just an example:
albumArtists = new AlbumsArtists();
albumArtists.Oid = Guid.NewGuid();
albumArtists.Album = db.Albums.Find(id);
albumArtists.Artist = db.Artists.Find(item.Artist.Oid);
//In the end it calls:
db.SaveChanges();
//Omitting other stuff...
}
On db.SaveChanges() within my DbContext:
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext() : base("name=DefaultConnection") { }
public System.Data.Entity.DbSet<ContentPub.Models.Music.Album> Albums { get; set; }
//Other DBSet objects...
public DbSet Set(string name)
{
return base.Set(Type.GetType(name));
}
public override int SaveChanges()
{
ApplicationLogDBContext logDb = new ApplicationLogDBContext();
ChangeTracker.DetectChanges();
ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;
List<ObjectStateEntry> objectStateEntryList =
ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added
| EntityState.Modified
| EntityState.Deleted)
.ToList();
foreach (ObjectStateEntry entry in objectStateEntryList)
{
Guid oid = Guid.Empty;
try
{
if (!entry.IsRelationship) //I don't understand this (first of my two questions)
{
switch (entry.State)
{
//Removed other cases
case EntityState.Modified:
{
oid = (Guid)entry.EntityKey.EntityKeyValues[0].Value;
//This is the area that I am having issues (second of the two questions)
//Below will work when I call db.SaveChanges() from the AlbumsController,
//'entry.Entity.ToString()' will get 'x.Models.Music.Albums' and begin a query
var query = this.Set(entry.Entity.ToString()).AsNoTracking().Where("Oid == #0", oid);
//The issue with the above is when I have a ViewModel, returns something like
// = System.Data.Entity.DynamicProxies.Album_AF81C390156ACC8283ECEC668AFB22C4AD621EF70F8F64641D56852D19755BF3
//If the proper Namespace is returned, the next line works and Audit continues
var query = this.Set(entry.EntitySet.ElementType.ToString()).AsNoTracking().Where("Oid == #0", oid);
//Does a bunch of AuditLog stuff if the above issue doesn't fail
break;
}
}
}
}
catch (Exception ex)
{
throw new Exception("Log Error (" + entry.Entity.ToString() + ") - " + ex.ToString());
}
}
return base.SaveChanges();
}
}
entry.Entity.ToString() will return something like:
System.Data.Entity.DynamicProxies.Album_AF81C390156ACC8283ECEC668AFB22C4AD621EF70F8F64641D56852D19755BF3
In the AlbumView I am updating Album, and a bunch of other Objects. Not sure why it isn't returning x.Models.Music.Albums, is there a work-around, can someone explain or point me to other resources that I haven't found yet?
While it isn't the most efficient solution, it still is a solution for now.
I was able to do the following inside my db.SaveChanges() method:
//When AlbumView .BaseType was able to return x.Models.Music.Album
string strNamespace = entry.Entity.GetType().BaseType.ToString();
//Needed this if I was updating just an Object (ie: Album),
//would be nice to make something more concret
if (strNamespace == "x.Models.Core.BaseObject")
strNamespace = entry.Entity.ToString();
//Continuing code
var query = this.Set(strNamespace).AsNoTracking().Where("Oid == #0", oid);
Found the answer here from another Question that I had not found before posting this question
The following method returns a IEnumerable MODEL called PROF.
Instead of using the model, I want to use a viewModel which contains the same fields as the Model PROF. How can i make changes to the following function to return my VIEWMODEL(which is called MyProfViewModel ) instead of the model (which is called PROF ).
public async Task<ActionResult> Index()
{
var cp= db.PROF.Include(c => c.ACCOUNTS);
return View(await cp.ToListAsync());
}
ViewModel
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes{ get; set; }
}
Model
public class PROF
{
public int ID{ get; set; }
public string DESCRIPTION { get; set; }
}
Joel's answer is pretty much it. But since you mentioned you are starting I will provide a more deltailed answer to make more clear how to use.
First you define a conversion, in this case I put it in the ViewModel, but you can put it somewhere else that would make more sense in your project:
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes { get; set; }
public static MyProfViewModel FromModel(PROF model)
{
var viewModel = new MyProfViewModel()
{
myprofID = model.ID,
myprofDes = model.DESCRIPTION
};
return viewModel;
}
}
Then you just apply the conversion before returning:
public async Task<ActionResult> Index()
{
var cp = PROF.Include(c => c.ACCOUNTS);
var models = await cp.ToListAsync();
var viewModels = models.Select(MyProfViewModel.FromModel);
return View(viewModels);
}
By the way, you can make the tranformation to models as well by adding this to your ViewModel class:
public PROF ToModel()
{
return new PROF()
{
ID = this.myprofID,
DESCRIPTION = this.myprofDes
};
}
Hope this helps!
Add a select statement which maps one type to another:
db.PROF.Include(c => c.ACCOUNTS)
.Select(x=> new MyProfViewModel(){myprofID = x.ID, ...});
Depending on if this select statement is supported by your ORM layer (Entity Framework?), I would do it before, or after .ListAsync();
Pros of before: could lead to better optimized SQL queries.
This question already has answers here:
There is already an open DataReader associated with this Command which must be closed first
(19 answers)
Closed 6 years ago.
Pretty much I have 2 models - A announcement where some can post an announcement and a seen model which determins if someone has seen the announcement. her eis the models:
Announce:
public class Announcement
{
public int AnnouncementId { get; set; }
public string AnnouncementContent { get; set; }
public virtual ApplicationUser User { get; set; }
}
and Seen:
public class Seen
{
public int SeenId { get; set; }
public virtual Announcement Announcement { get; set; }
public virtual ApplicationUser User { get; set; }
}
in my AnnouncementController.Index I have this code which pretty much supposed to be, if you view this page, mark off every announcement as seen bbut am getting errors at the "new seen" part:
public ActionResult Index()
{
string currentUserId = User.Identity.GetUserId();
var currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
if(db.Announcements != null)
{
foreach (Announcement anoun in db.Announcements)
{
new Seen
{
User = db.Users.Add(currentUser),
Announcement = db.Announcements.FirstOrDefault(x => x.AnnouncementId == anoun.AnnouncementId),
};
}
}
return View(db.Announcements.ToList());
}
There is already an open DataReader associated with this Command which must be closed first.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Announcement> Announcements { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Seen> Seens { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Event if you did not have that error, you still have other issues. See below:
public ActionResult Index() {
string currentUserId = User.Identity.GetUserId();
var currentUser = db.Users.FirstOrDefault( x => x.Id == currentUserId );
List<Seen> seens = new List<Seen>();
if( db.Announcements != null ) {
foreach( Announcement anoun in db.Announcements ) {
seens.Add(
new Seen
{
User = currentUser, // You have this already so why go to the database again?
Announcement = anoun, // Same with this.
});
}
}
// Save seens to the database
return View( db.Announcements.ToList() );
The error is because you are actively iterating over a collection streaming from the database and then trying to re-issue another select to the database inside that loop. It can be fixed by forcing the materialization of the collection before you enter the loop using ToList() (other options are also available like AsEnumerable or ToArray).
foreach (Announcement anoun in db.Announcements.ToList())
{
new Seen
{
User = db.Users.Add(currentUser),
Announcement = db.Announcements.FirstOrDefault(x => x.AnnouncementId == anoun.AnnouncementId),
};
}
That being said I am not sure why you are doing it this way. Why not attach the instance anoun directly as you are only using a single (or same in the code shown) DbContext instance (variable named db).
foreach (Announcement anoun in db.Announcements)
{
new Seen
{
User = currentUser, // also this seemed wrong before, just assign the reference directly
Announcement = anoun
};
}
Or make it even more simple:
var newSeens = db.Announcements.Select(x => new Seen(){User = currentUser, Announcement = x}).ToList();
db.Seens.AddRange(newSeens);
db.SaveChanges();
This assumes that the user has not seen any announcement. If the user has seen some then you need to filter db.Announcements on existing Seen records for that user.
For simplicity and testing, i'm doing this from a new project and then apply it to my actual project once i understand the problem.
I made a model, named Person, which contains a List property, named ServiceNeeded. Users in the front end may encode as much string of services as they wish, so the input field for ServiceNeeded is dynamically created. In the POST method, those string input binds as expected. I save the Person object into the database, and works as expected. When I try to retrieve the people objects from the database, all but ServicesNeeded are present.
Here are my codes
Model (Person.cs):
public class Person
{
public Guid Id { get; set; }
public String Name { get; set; }
public List<String> ServiceNeeded { get; set; }
public Person()
{
this.ServiceNeeded = new List<String>();
}
}
Controller(Index and [POST]Create methods):
public ActionResult Index()
{
var x = db.People.ToList();
return View(db.People.ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,ServiceNeeded")] Person person)
{
if (ModelState.IsValid)
{
person.Id = Guid.NewGuid();
db.People.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
As I said the input fields for ServicesNeeded are dynamically create but properly bind to the model.
Here are some screenshots during runtime:
After user do some input:
The I added a variable before Index returns the View to check in runtime:
As seen, the same Person is present and everything but ServicesNeeded. I am fairly new with ASP.NET MVC and web development in general. I do handle other collections in my project, although are collection of objects. Only this particular case I do not understand. Where could my error be?
You need to tell the dbcontext to load the references if lazy loading is not enabled.
First change ServiceNeeded to a virtual property
public virtual List<String> ServiceNeeded { get; set; }
then load the property when you pull down the person by using Include
var x = db.People.Include("ServiceNeeded").ToList();
return View(x);
Article on loading related entities https://msdn.microsoft.com/en-us/data/jj574232.aspx
Look in the database; are the records there? I think the issue is that the ServicesNeeded collection is not persisted to the database; try adding:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,ServiceNeeded")] Person person)
{
if (ModelState.IsValid)
{
person.Id = Guid.NewGuid();
db.People.Add(person);
foreach (var service in person.ServicesNeeded)
db.ServicesNeeded.Add(service);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
I'm don't believe the relationships attached to the Person object auto-persist on SaveChanges(), and are probably not making it to the database.
I think the best idea that suits Entity Frame work is to create another Model for your 'ServiceNeeded', add it to your 'DbContext', save any services in the corresponding DbSet and retrieve them using .Include in LINQ. After this lengthy introduction look at the following codes:
In your Models:
public class MyService{
public Guid Id {get;set;}
public string ServiceName {get;set;}
public Guid PersonId {get;set;}
}
public class Person
{
public Guid Id { get; set; }
public String Name { get; set; }
public virtual IList<MyService> ServiceNeeded { get; set; }
}
In your ApplicationDbContext:
public System.Data.Entity.DbSet<MyService> MyServices { get; set; }
public System.Data.Entity.DbSet<Person> People { get; set; }
In your POST ActionResult:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,ServiceNeeded")] Person person, List<String> ServiceNeeded)
{
if (ModelState.IsValid)
{
db.People.Add(person);
db.SaveChanges();
db.Entry(person).GetDatabaseValues();
foreach (string service in ServiceNeeded){
db.MyServices.Add( new MyService {ServiceName = service,
PersonId = person.Id})
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
Please note that I removed person.Id = Guid.NewGuid(); so that the Id
be generated by Database Identity and then I re-get the person.Id so that I create the MyService record.
and finally as #JamieD77 suggested, in your GET ActionResult:
public ActionResult Index()
{
var x = db.People.Include("ServiceNeeded").ToList();
return View(x);
}
I have created an ASP.NET MVC5 sample project. I created my entities and from that, scaffolded the controllers for CRUD operations. I can only edit the POD members with the scaffolded code. I want to be able to add/remove related entities.
With my current code, when I click save there is no error but no related entities are modified (POD data is modified though). For example, if I wanted to remove all players from the account, they aren't removed. What am I doing wrong?
How can I remove/add related entities and push those changes to the database?
Here is the form:
Here is the action to update the entity:
public async Task<ActionResult> Edit([Bind(Include = "Account,Account.AccountModelId,Account.Name,Account.CreatedDate,SelectedPlayers")] AccountViewModel_Form vm){
if (ModelState.IsValid){
if (vm.SelectedPlayers != null){
vm.Account.PlayerModels = db.PlayerModels.Where(p => p.AccountModel.AccountModelId == vm.Account.AccountModelId).ToList();
foreach (var player in vm.Account.PlayerModels){
player.AccountModel = null;
db.Entry(player).State = EntityState.Modified;
}
vm.Account.PlayerModels.Clear();
foreach (var player_id in vm.SelectedPlayers){
var player = db.PlayerModels.Where(p => p.PlayerModelId == player_id).First();
vm.Account.PlayerModels.Add(player);
db.Entry(player).State = EntityState.Modified;
}
}
db.Entry(vm.Account).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(vm);
}
Here are the models:
public class AccountViewModel_Form{
public AccountModel Account { get; set; }
public HashSet<Int32> SelectedPlayers { get; set; }
public virtual List<PlayerModel> PlayersList { get; set; }
}
public class AccountModel{
public AccountModel(){
PlayerModels = new HashSet<PlayerModel>();
}
public Int32 AccountModelId { get; set; }
public string Name { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ICollection<PlayerModel> PlayerModels { get; set; }
}
public class PlayerModel{
public Int32 PlayerModelId { get; set; }
public float Gold { get; set; }
public string Name { get; set; }
public virtual AccountModel AccountModel { get; set; }
}
I'm basically lost. I can't find any examples in how to update related data. Could someone point me in the right direction?
I come from Symfony (PHP Framework) background. I thought it would be easier but I have been having problems.
Basically I was missing the Attach function and that I had to force the load on the collection to make it work.
I found how to attach a non-attached entity here: Model binding in the controller when form is posted - navigation properties are not loaded automatically
When you post the data, the entity is not attached to the context, and when you try to save changes to a complex entity, the context makes a mess.
The code is a little different because I was trying to make it work at home. But it is essentially the same models.
public ActionResult Edit(AccountEditViewModel vm)
{
if (ModelState.IsValid)
{
//I was missing these 2 important lines...
db.Accounts.Attach(vm.Account);
db.Entry(vm.Account).Collection(a => a.Players).Load();
if (vm.SelectedPlayers != null)
{
foreach (var player in vm.Account.Players.ToList())
{
if (vm.SelectedPlayers.Contains(player.Id) == false)
{
player.Account = null;
vm.Account.Players.Remove(player);
db.Entry(player).State = EntityState.Modified;
vm.SelectedPlayers.Remove(player.Id);
}
}
foreach (var player_id in vm.SelectedPlayers)
{
var player = db.Players.Where(p => p.Id == player_id).First();
player.Account = vm.Account;
vm.Account.Players.Add(player);
db.Entry(player).State = EntityState.Modified;
}
}else
{
vm.Account.Players.Clear();
}
db.Entry(vm.Account).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(vm);
}