Scaffolding with Entity Framework in ASP.NET MVC 4 - asp.net

I'm following along with "Professional ASP.NET MVC 4" and trying to generate a controller from a model using Entity Framework. My Model looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcMusicStore.Models
{
public class MusicStoreModels
{
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
}
}
When I right click my Controllers folder and choose Add > Controller I select "MVC controller with read/write action and views, using Entity Framework" as my template and "Album (MvcMusicStore.Models)" as my Model class. The book tells me to select "new data context..." and name it "MvcMusicStore.Models.MusicStoreDBContext".
Everything looks OK and I have saved and built my solution prior to performing the above actions. However, I get an error message saying
There was an error generating 'MvcMusicStore.Models.MusicStoreDBContext'.
Try rebuilding your project.'
I'm at a bit of a loss. Can anyone help?

The symptoms sound a lot like this issue:
http://www.rhysgodfrey.co.uk/archive/2011/04/20/mvc3-tools-update-and-entity-framework-4-1-error.aspx
I would suggest uninstalling all versions of Entity Framework, re-installing the latest version through NuGet, and regenerating your EF context from scratch.

I not very up on C# (i think it is C# code - but dont quote me on that).....
Well after pasting your code in a new project i notice the line that says
Using System.Data.Entity;
Is flagged as an error,
And some further research shows that a refrence is POSSIBLY missing in inyour config.sys file...
Re: http://forums.asp.net/t/1381740.aspx/1

Related

MVC ASP.net definition GetEnumerator error

So i get the error message in the title and i dont understand why.
Heres how i reference the model in the view
#model IEnumerable<Lowflix.Models.LendingIndexModel>
The errors coming up in this foreach
#foreach (var item in Model)
Really cant explain to myself how this i get this error since i even declared IEnumerable. It contains multiple objects.
This is the error message:
foreach statemetn cannot operate on variables
of type "Models" because "Models" does not
contain a public instance of GetEnumerator
-
model:
using Lowflix.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Lowflix.Models
{
public class LendingIndexModel
{
public LendingIndexModel()
{
}
public Guid LendingId { get; set; }
public Guid CustomerId { get; set; }
public Guid CopyId { get; set; }
public String Title { get; set; }
public DateTime StartDate { get; set; }
public DateTime? ReturnDate { get; set; }
public virtual Movie Movie { get; set; }
public virtual Customer Customer { get; set; }
public virtual Copy Copy { get; set; }
}
debugging model:
model type:
+ Model {System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending,
Lowflix.Models.LendingIndexModel>} System.Collections.Generic.IEnumerable
<Lowflix.Models.LendingIndexModel>
{System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending,
Lowflix.Models.LendingIndexModel>}
Please try with below code.
#model List<Lowflix.Models.LendingIndexModel>
In the View page change IEnumerable to List.

Cannot migrate AspNetUser entity in Entity Framework Code First Approach in .NET

I am developing an ASP.NET MVC project using Visual Studio 2013. I am using Entity Framework Code First Approach to interact with database. But I am having a problem with migrating data because of built-in identity system in ASP.NET. Everything was fine before I touch to identity system.
These are the steps I have done.
I registered an account from UI using built-in identity system. So AspNetUsers table is created in database.
I created AspNetUser class to my code because I need to work with it.
Then I run migration and update database command. It throws error.
This is my AspNetUser class
public class AspNetUser
{
[Required]
public String Id { get; set; }
[Required]
public String UserName { get; set; }
public String PasswordHash { get; set; }
public String SecurityStamp { get; set; }
[Required]
public String Discriminator { get; set; }
}
This is my db context class
public class AyarDbContext : DbContext
{
public AyarDbContext()
: base("DefaultConnection")
{
}
public DbSet<Category> Categories { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<Area> Areas { get; set; }
public DbSet<Item> Items { get; set; }
public DbSet<ItemContactInfo> ItemContacts { get; set; }
public DbSet<Gallery> Galleries { get; set; }
public DbSet<Mail> Mails { get; set; }
public DbSet<AspNetUser> AspNetUsers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
This is the error I got.
There is already an object named 'AspNetUsers' in the database.
How can I migrate AspNetUser class? I registered using UI because I auto create the other tables need for identity system. How can I migrate that class and map with AspNetUsers table that is already existing in database.
As the error explains, that table already exists and is accessible in this way:
var user = context.Users.First(u => u.Id == myId);
If you want to extend the user class and add properties, you can inherit it:
public class MyAppUser : IdentityUser
{
// don't include properties already in IdentityUser
public string MyNewProperty { get; set; }
}
http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/

Implement one-to-many relationship in ASP.NET MVC 5

This is my project model :
public class Project
{
public int ProjectID { get; set; }
public string ProjectTitle { get; set; }
public string ProjectDetails { get; set; }
public virtual ICollection<Proposal> Proposals{ get; set; }
}
This is my Proposal model :
public class Proposal
{
public int ProposalID { get; set; }
public string BidTitle { get; set; }
public string BidDetails { get; set; }
public virtual Project Project { get; set; }
}
As you can see, there is one-to-many relationship between Project and Proposal. In
mydomain/Project/Details/ProjectID
view, I want to put a button, when this button is clicked, user can create a new Proposal for that project. My question is how I can pass that project's information to bid? If you can give me some tips about it, I'd be really glad. Thanks.
Create a model known as a viewmodel, which includes both the models you want to use under the same view. Your would look something like this:
public class ProposalAndProjectModel
{
public Proposal Proposal { get; set; }
public Project Project{ get; set; }
}
Save it as something like ProposalAndProjectModel.cs and then in your view, reference this model.
Now in your view you will be able to do the following:
Model.Proposal.propertyName
or
Model.Project.propertyName
This should help you as for getting the correct parameters for creating new objects.
You say when user click button user goto another page. You can sen projectID as get to that page. Thats how you can get that projectID.

add-migration not generating anything

So I've tried running "add-migration AddBooking -verbose"
To this class:
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace HostelBookingSystem.Models
{
public class Booking
{
[Key]
public Int32 BookingId { get; set; }
public Int32 Duration { get; set; }
public Double Price { get; set; }
public BookingStatus Status { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual Bunk Bunk { get; set; }
public virtual Room RoomPreference { get; set; }
}
}
However I'm receiving completely empty Up() and Down() methods.
The same thing happens when I remove the 3 public virtual attributes as well.
Can any explain why this is?
1) The DBContext class needs to be set up
2) Include the property for the above class in the DBContext
public DbSet<Booking> Books { get; set; }
The reason this didn't generate anything was because I didn't have a DbContext set up for the class I wished to generate a table for.

ForeignKey not being recognised in VS2012 RC

after a lot of help yesterday, I came up against a known error in asp.net4 beta - I upgraded to VS2012 RC Express (4.5), and now VS is reporting two errors in my model, which were ok previously:
"The type or namespace name 'ForeignKeyAttribute' could not be found (are you missing a using directive or an assembly reference?)"
"The type or namespace name 'ForeignKey' could not be found (are you missing a using directive or an assembly reference?)"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
namespace MvcApplication6.Models
{
public class tblRental
{
[Key()]
public int rental_id { get; set; }
public int room_id { get; set; }
public DateTime check_in { get; set; }
public DateTime check_out { get; set; }
public decimal room_cost { get; set; }
public long customer_ref { get; set; }
[ForeignKey("customer_ref")]
public virtual tblCustomerBooking Customer { get; set; }
}
public class tblCustomerBooking
{
[Key()]
public long customer_id { get; set; }
public string customer_name { get; set; }
public string customer_email { get; set; }
public virtual ICollection<tblRental> Rentals { get; set; }
}
Does anyone know if the ForeignKey reference has been changed?
Thanks for any help,
Mark
I just figured out I needed to add:
using System.ComponentModel.DataAnnotations.Schema;
I didn't need to move it before, so I assume ForeignKey has been moved under the schema namespace.
Hope this helps anyone else,
Thanks, Mark

Resources