0

I need to configure a relation with following classes: User have or not a profile. Structure: User: UserID, Username, Password Profile: UserID, FullName, Address, Phone

public class User
{
    #region Feilds
    public int UserID { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    #endregion

    public virtual Profile Profile { get; set; }
}

public class Profile
{
    #region Fields
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    #endregion

    public virtual User User { get; set; }
}

Configuration:

public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
        : base()
    {
        // Primary Key
        this.HasKey(p => p.UserID);

        //Foreign Key
        this.HasOptional(p => p.Profile).
            WithMany().
            HasForeignKey(p => p.UserID);
    }
}

Error:

System.Data.Edm.EdmEntityType: : EntityType 'Profile' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Profiles� is based on type �Profile� that has no keys defined.

Please help.

Thanks.

Sohail
  • 45
  • 1
  • 6

1 Answers1

1

Each entity in Entity framework must have primary key defined. Your profile entity should look like to get out of your current error:

public class Profile
{
    #region Fields
    public int Id {get;set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    #endregion

    public virtual User User { get; set; }
}
Bishnu Paudel
  • 2,083
  • 1
  • 21
  • 39
  • Got it, but what if we don't want to add a primary key. Like this is a table where logically I don't need a primary key, because every profile will be relevant to some specific user? defining key for such will be overhead? please correct if I am wrong. Thanks. – Sohail Sep 24 '12 at 13:12
  • 1
    Not having a primary key is a bad design practice. Entity framework needs primary key on each entity to track the entities in the context hence it is a must when using Entity framework. – Bishnu Paudel Sep 24 '12 at 13:16
  • I have another issue with same database which is confusing me, though both are relevant. I have a junction(M to M) table named as ATTEMPTED_QUESTIONS(TestCode, CandidateID, QuestionID, GivenAnswer, Marks) which is mapping more than two entities and having some custom properties like (GivenAnswer, Marks). I don't need a primary key there and EF also giving same error for this table. What should i do? Please suggest. – Sohail Sep 24 '12 at 13:19
  • The overhead of having a key defined isn't as important as the performance gain of having an indexed primary key field. http://stackoverflow.com/questions/840162/should-each-and-every-table-have-a-primary-key It's good database design to have a key defined for every table. – Mike Evans Sep 24 '12 at 13:23
  • Do the same with your junction table, define a primary key (you may create auto identity column) even though you don't need it, Entity framework needs it. If you were to have just the foreign keys in the junction table, you wouldn't need the table at all. – Bishnu Paudel Sep 24 '12 at 13:25
  • @BishnuPaudel: Agreed but what to do then if I also need the custom properties as I have defined above. – Sohail Sep 24 '12 at 13:27
  • @Sohail, all good, define an entity with all the custom properties & foreign keys and don't forget to define primary key :) – Bishnu Paudel Sep 24 '12 at 13:29
  • @BishnuPaudel Error Comes UP :P One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'User_Profile_Target' in relationship 'User_Profile'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'. System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'User_Profile_Source' in relationship 'User_Profile'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the – Sohail Sep 24 '12 at 13:51
  • have a look at this answer http://stackoverflow.com/questions/8007129/codefirst-ef4-1-mvc-against-legacy-database-multiplicity-conflicts. – Bishnu Paudel Sep 24 '12 at 22:46