Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:37:56+00:00 2026-06-03T21:37:56+00:00

I have three classes: public partial class Student : Contact { //Inherited from Contact:

  • 0

I have three classes:

public partial class Student : Contact
{   
    //Inherited from Contact:
    //public int ContactId { get; set; }
    //public string FirstName { get; set; }
    //public string LastName { get; set; }

    public virtual StudentExam StudentExam { get; set; }
}

public partial class Exam
{
    public int ExamId { get; set; }
    public string Title { get; set; }
    public DateTime Date { get; set; }

    public virtual StudentExam StudentExam { get; set; }
}

public partial class StudentExam
{
    public byte Score { get; set; }
    public int ContactId { get; set; }
    public int ExamId { get; set; }

    public virtual Student Student { get; set; }
    public virtual Exam Exam { get; set; }
}

When trying to initialize the DbContext, it throws a ModelValidationException:
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType ‘StudentExam’ has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet ‘StudentExams’ is based on type ‘StudentExam’ that has no keys defined.

I tried changing the StudentExam class’ properties to the following:

[Key, ForeignKey("Student"), Column(Order = 0)]
public int ContactId { get; set; }
[Key, ForeignKey("Exam"), Column(Order = 1)]
public int ExamId { get; set; }

Now I get this exception:

\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role ‘StudentExam_Student_Source’ in relationship ‘StudentExam_Student’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be ‘*’.
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role ‘StudentExam_Exam_Source’ in relationship ‘StudentExam_Exam’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be ‘*’.

Is there any way to achieve this in with data annotations (I don’t like using the fluent API when I can use data annotations; The fluent API leads to messy code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:37:58+00:00Added an answer on June 3, 2026 at 9:37 pm

    It is not about data annotations or fluent api but about incorrectly defined classes – relations defined in your classes cannot be mapped at all because they are not valid at relational level. You must modify your classes:

    public partial class Student : Contact
    {   
        public virtual ICollection<StudentExam> StudentExams { get; set; }
    }
    
    public partial class Exam
    {
        ...
    
        public virtual ICollection<StudentExam> StudentExams { get; set; }
    }
    

    Once you have this relations defined you can use your data annotations for defining keys in StudentExam class and it will work.

    Btw. fluent api doesn’t lead to messy code. Messy code is created by programmer, not by API. Data annotations in turn violates POCO principle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have three classes: class X{}; class Y{}; class Both : public X,
I have three classes; Classes A and B both reference class C . How
If I have three classes class A class B extends A class C extends
I have an entity that looks like this: public partial class MemberTank { public
In my application's Business Logic layer I have the following classes: public class EocMonitor
Is there a way to force classes in Java to have public static final
Have three classes User, Group and Field. Many to many relationship on User /
I have three classes (for example) Buick implements Vehicle Chevy implements Vehicle Ferrari implements
I have three classes that all have a static function called 'create'. I would
I have three classes: ClassA , ClassB , ClassC . ClassC extends ClassB which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.