ForeignKey not being recognised in VS2012 RC - asp.net

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

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.

mvc inconsistent accessibility property

I'm new to all this but I could not find the answer. (i'm sorry for my noobyness)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Myproject.Models
{
public class Product
{
public int ProductID { get; set; }
[Required]
public string Name { get; set; }
public string Weight { get; set; }
public List<Options> **Options** { get; set; }
public List<Variants> **Variants** { get; set; }
}
}
(I have started the bit that has blue underlines)
I have two other models an options.cs model and a variants.cs model.
The thing is that I get this error message on the options and variants text after the list.
I have been following the MVC 02 | Developing ASP.NET MVC 4 Models tutorial and when making the model Chris uses public List<Sessions> Sessions { get; set; } and he does not get any blue underlines...
My question is why am i getting "inconsistent accessibility property"
Chris is using 2012 and I'm using 2013 visual studios. So it has something to do with that but why? and how do i fix it?
I'm getting the same error on my DbSet Options { get; set; } and my Variants DbsSet
Thanks for the help.
In the options.cs and variants.cs files mark your classes as public:
public class Options { ... }
public class Variants { ... }
In public class you can't expose public property of type which is not public, because in this scenario the other external assemblies can view you public property but if the class is not public this type is unknown for them.

EF CodeFirst ,how to add custom model to create table?

I work on ASP.NET MVC 5.1 and follow this post to create my own UserProfile table by Table Per Type (TPT) method.
But how do i want to add another model by code first?
For example, i want to add independent table Country table and Currency table,
I would create the following classes:
Country.cs
namespace MiaPos.Models
{
public class Country
{
// Primary key
public int CountryID { get; set; }
[Required]
public string Name { get; set; }
}
}
Currency.css
namespace MiaPos.Models
{
public class Currency
{
// Primary key
public int CurrencyID { get; set; }
[Required]
public string Name { get; set; }
}
}
DataContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
namespace MiaPos.Models
{
public class DataContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<Currency> Currencies { get; set; }
}
}
I place above of them on the Models folder,
but how can i recognize by CodeFirst Add-Migration command on Nuget Console?
I saw some post say EF will auto generate the migration class if i inherit System.Data.Entity.DbContext class.

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.

Scaffolding with Entity Framework in ASP.NET MVC 4

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

Resources