I'm using VS 2010 with Razor/MVC 3 to create a form where User 1 submits information to a database using RequestQueue Model (this is just a queue table). User 2 then pulls this information, approves it, and the data is sent to the main database via DbRequestForm Model. This model, however, consists of several sub-models (Data_Dictionary, SERVER_DATABASE, and DatabaseRequestInfo) because the submitted data needs to go to three separate tables.
The problems is, whenever I approve the data for submission from RequestQueue to DbRequestForm, I get an error saying there is no primary key for DbRequestForm. However, there is a PK for each sub-model. I have also tried adding [Key] public int SrvrDB_ID { get; set; } and other variations to DbRequestForm to no avail.
Initial Model:
public class RequestQueue
{
[Key]
public int SrvrDB_ID { get; set; }
public string dbName { get; set; }
public int InitialSpaceNeeded { get; set; }
public string ScheduledJobSetup { get; set; }
public string PII_data { get; set; }
}
Encapsulating Model:
public class DbRequestForm
{
public Data_Dictionary Data_Dictionary { get; set; }
public DatabaseRequestInfo DatabaseRequestInfo { get; set; }
public SERVER_DATABASE SERVER_DATABASE { get; set; }
}
Models based on final three tables:
public class Data_Dictionary
{
[Key]
public int SrvrDB_ID { get; set; }
public string PII_data { get; set; }
}
public class SERVER_DATABASE
{
[Key]
public int SrvrDB_ID { get; set; }
public string dbName { get; set; }
}
public class DatabaseRequestInfo
{
[Key]
public int SrvrDB_ID { get; set; }
public int InitialSpaceNeeded { get; set; }
public string ScheduledJobSetup { get; set; }
}
Any ideas how to avoid this error? Thanks in advance!
EDIT: The specific error is '
System.Data.Edm.EdmEntityType: : EntityType 'DbRequestForm' has no key
defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet DbRequestForms is
based on type DbRequestForm that has no keys '
The error tells that you need to define key for DBRequestForm.
Code First would infer that a property is a primary key if the property is called ‘Id’ or ‘class name Id’.
In your case you have to add
[Key]
public int DBRequestFormId { get; set; }
This concrete error won't appear anymore.
here is detailed information how code first works
http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx
see the Primary Key section
Related
i am trying to define a database model in code-first to see and display which user is assigned as a specialist for the record data.
I have a very simple model for the user:
public class User
{
public int ID { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
....
}
Next I have defined two (simple) models which define the data that can be edited by the user and the specialist should be assigned to using a dropdownlist:
public class Order
{
public int ID { get; set; }
public string orderNumber { get; set; }
public int specialistID { get; set; }
public virtual User specialist{ get; set; }
}
public class Part
{
public int ID { get; set; }
public string partNumber { get; set; }
public string description { get; set; }
public int specialistID { get; set; }
public virtual User specialist{ get; set; }
}
What kind of relation between the models can be used without having a navigation property for each table in the User model?
Do I need to use additional tables to define the relationship: User.Id-Order.specialistID and the relationship: User.Id-Part.specialistID ?
Is there a smarter way out-of-the-box by Entity Framework?
Many thanks for your answers.
Pascal
By default when you add forign-key constraint to the many-to-one table the Entity Framework add virtual property to the entity class and virtual ICollection to the User.
I've got a table called Pages which has a primary Key of PageID. I want to use the following model to make at table called PageMap which gets the current and next PageID (one is going to be a link) but I keep getting an error with the foreign key. Any help would be greatly appreciated!
public class PageMap
{
public int PageMapID { get; set; }
[Required]
[DisplayName("Page Visible On")]
public int PageID { get; set; }
[Required]
[DisplayName("Question")]
public int QuestionID { get; set; }
[Required]
[ForeignKey("PageID")]
[DisplayName("Link to Page")]
public int NextPageID { get; set; }
[DisplayName("Ordering")]
public int Order { get; set; }
public virtual Pages Pages { get; set; }
public virtual Questions Questions { get; set; }
}
Try putting the name of the entity in foreign key attribute.i.e
[ForeignKey("Page")]
I am buys designing the model below:
public class LogModel
{
public class UserActivityLogs
{
[Key]
public int id { get; set; }
//Id of the user
public string userId { get; set; }
//Time of the log
public DateTime time { get; set; }
public LogActions action { get; set; }
}
// Types of actions to log
public class LogActions
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
}
}
Now what I would like to know is do I need to add a table in the context for Logactions as well as UserActivityLogs or will EF see that the two tables are linked and create the log action table automatically?
Also have I specified my relationships correctly? What I was aiming for is that I can define multiple types of Logactions and then a userlog will then have a single log action associated to it.
First, don't use nested classes, it's a needless complication. Use namespaces to organize classes.
Second, don't use plural names for classes. One instance of class represents one entity. Also, use CamelCase names for properties.
Third, yes, Entity Framework will be aware of the associations between the two classes and create a database model with two tables and a foreign key.
So this leaves you with:
namespace MyApp.LogModel
{
public class UserActivityLog
{
[Key]
public int Id { get; set; }
public string UserId { get; set; }
public DateTime Time { get; set; }
public LogAction LogAction { get; set; }
}
public class LogAction
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
I am a MVC newbie so go easy on me please.
I am getting two errors when I try to add a migration. They are as follows:
EntityType 'Icon' has no key defined. Define the key for this EntityType.
EntityType: EntitySet 'Icons' is based on type 'Icon' that has no keys defined.
I am including the Icon inside another model, like so:
public class Icon
{
public string IconName { get; set; }
public string IconColor { get; set; }
public int BackgroundXPos { get; set; }
public int BackgroundYPos { get; set; }
public string IconColorHover { get; set; }
public int BackgroundHoverXPos { get; set; }
public int BackgroundHoverYPos { get; set; }
}
public class GalleryThumbnail : CSSBoxModel
{
[DisplayName("Thumbnail Image Outline Color")]
public string ThumbnailImageOutlineColor { get; set; }
[DisplayName("Thumbnail Menu Font")]
public CSSFont ThumbnailMenuFont { get; set; }
[DisplayName("Thumbnail Icon Color")]
public Icon ThumbnailIconColor { get; set; }
}
How is this Address class below any different which is working:
public class Address
{
public String Adress1 { get; set; }
public String Adress2 { get; set; }
public String Adress3 { get; set; }
public String City { get; set; }
public String County { get; set; }
public String State { get; set; }
public String Zip { get; set; }
public String Country { get; set; }
}
[Table("UserProfile")] //Could be PP empolyee, Subscriber or Subscriber's customer
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public bool? Gender { get; set; }
public Address Address { get; set; } //billing address
public Address ShipAddress { get; set; }
}
I did not add a key in either my Icon or Address class because I have no intention of storing specific data in my DB. They are merely to be used inside other classes. So wy is one neededing an ID and the other is not?
I have not created public DbSet Icons { get; set; } in my DB Context either.
Also can you tell me what it is called when you use a class inside another ( or instance of class inside a class as in these examples ) ?
Much appreciated!
Since the address entity has no key defined it the Entity Framework assumes it's a complex property, and your UserProfile table will be rendered with columns named Addres_Address1, Address_Address2, Address_Address3, Address_City, and so on...
Even though you haven't declared an EntitySetIcons DbSet on your context class, it's still being added implicitly because one of your other classes somewhere has an ICollection or IEnumerable property defined.
More info on Code Conventions here:
http://msdn.microsoft.com/en-us/data/jj679962.aspx
So, either decorate the collections as NotMapped like #Kamyar said or simply remove the references from any class already declared as a DbSet.
you can use [NotMapped] attribute in System.ComponentModel.DataAnnotations.Schema namespace in EntityFramework.dll:
using System.ComponentModel.DataAnnotations.Schema;
...
[NotMapped]
public Address Address { get; set; } //billing address
[NotMapped]
public Address ShipAddress { get; set; }
Regarding the naming, AFAIK these are called public properties as well.
I've created a standard MVC 4 application using the built in account controllers/models/views. I've now tried to add another controller but I want to create a foreign key relationship to an existing user, but im getting the error message:
The ForeignKeyAttribute on property 'CreatedBy' on type 'MVC4App.Models.ListingModel' is not valid. The foreign key name 'UserId' was not found on the dependent type 'MVC4App.Models.ListingModel'. The Name value should be a comma separated list of foreign key property names.
The Listing Model Code is:
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace MVC4App.Models
{
public class ListingModel
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Location { get; set; }
public string Instrument { get; set; }
public string Genres { get; set; }
public string Description { get; set; }
[ForeignKey("UserId")]
public virtual UserProfile CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
}
}
What am I missing here? Or is there a better way I should be doing this?
Also I'm assuming I can just add extra properties to the user profile such as an email address or a link to an avatar image later on in the project?
You have to add the UserId to your entity:
namespace MVC4App.Models
{
public class ListingModel
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Location { get; set; }
public string Instrument { get; set; }
public string Genres { get; set; }
public string Description { get; set; }
public int UserId {get; set;}
[ForeignKey("UserId")]
public virtual UserProfile CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
}
}