locked
deal code first approach relationship RRS feed

  • Question

  • hi,

    I am using Code first approach in my asp.net mvc 4 application. (using vs2012).

    I am new to it so that's why ii am facing some problem here.

    Please guide.

    below are my actual classes and their relationships.

     public class SchoolManagementSystemDB : DbContext
        {
            public SchoolManagementSystemDB()
                : base("name=DefaultConnection")
            {
            }
    
            public DbSet<Student> Students { get; set; }
            public DbSet<Address> Addresses { get; set; }
        }

    and entities are.

      public class Person
        {
            public virtual MandatoryInformation MandatoryInformation
            {
                get;
                set;
            }
    
            public virtual BasicInformation Basic
            { get; set; }
    
            public virtual Address Address
            {
                get;
                set;
            }
                 
    
            public virtual ContactInformation ContactInformation
            {
                get;
                set;
            }
    
            public virtual LibraryModule LibraryInformation
            {
                get;
                set;
            }
    
            public virtual AttendenceInformation AttendenceInformation
            {
                get;
                set;
            }
        }

      public class Student : Person
        {
            [Key]
            public UInt64 ID { get; set; }
    
            public virtual String RollNo
            {
                get;
                set;
            }
    
            public virtual ClassModule  StudentClass 
            { 
                get; 
                set;
            }
            public virtual FeeInformation FeeInformation
            {
                get;
                set;
            }
    
            public virtual String Section
            {
                get;
                set;
            }       
           
        }
      public class ClassModule
        {
            [Key] 
            public virtual int ID { get; set; }
            public virtual String Name
            {
                get;
                set;
            }
    
            public virtual String Section
            {
                get;
                set;
            }
    
            public virtual String SubGroup
            {
                get;
                set;
            }
    
            public virtual Int64? StudentsQuantity
            {
                get;
                set;
            }
    
            public virtual ICollection<Student> Student
            {
                get;
                set;
            }
    
            public virtual ICollection<Subject> Subject
            {
                get;
                set;
            }
    
            public virtual ResultCard ResultCard
            {
                get;
                set;
            }
    
            public virtual Teacher Teacher
            {
                get;
                set;
            }
    
        }

     public class FeeInformation
        {
    
            public virtual Decimal? StudentFee
            {
                get;
                set;
            }
    
            int frequency;
            public virtual Frequency FeeFrequency 
            {
                get { return (Frequency)frequency; }
                set { frequency = (int)value; }
            }
    
            public virtual DateTime? ChallanFeeDate { get; set; }
    
            public virtual DateTime? FeeSubmissionDate { get; set; }
    
            public int LateFeeDays { get; set; }
    
            public virtual Decimal? DiscountFee
            {
                get;
                set;
            }
    
            public virtual Decimal? ExtraFunds
            {
                get;
                set;
            }
    
            public virtual Decimal? StudentFine
            {
                get;
                set;
            }
        }

      public class ResultCard
        {
            public virtual int ExamID
            {
                get;
                set;
            }
    
            public virtual String ExamName
            {
                get;
                set;
            }
    
            public virtual String SemesterOrTerm
            {
                get;
                set;
            }
    
            public virtual DateTime? ProcessingDate
            {
                get;
                set;
            }
    
            public virtual double? Marks
            {
                get;
                set;
            }
    
            public virtual double? ObtainedMarks
            {
                get;
                set;
            }
    
            public virtual double? TotalMarks
            {
                get;
                set;
            }
    
            public virtual double? TotalObtainedMarks
            {
                get;
                set;
            }
    
            public virtual String Grade
            {
                get;
                set;
            }
    
            public virtual double? Percentage
            {
                get;
                set;
            }
    
            public virtual double? GPA
            {
                get;
                set;
            }
    
            public virtual double? CGPA
            {
                get;
                set;
            }
        }

     public class Teacher : Person
        {
            public virtual Int32 ID
            {
                get;
                set;
            }
            
            public virtual String Barcode
            {
                get;
                set;
            }      
          
    
    
            int likenessByStudents;
            public virtual Rating LikenessByStudents
            {
                get { return (Rating)likenessByStudents; }
                set { likenessByStudents = (int)value; }
            }
            
            int likenessByFaculty;
            public virtual Rating LikenessByFaculty
            {
                get { return (Rating)likenessByFaculty; }
                set { likenessByFaculty = (int)value; }
            }
    
           
    
            public virtual String Qualification
            {
                get;
                set;
            }
    
            int role;
            public virtual Roles Role
            {
                get { return (Roles)role; }
                set { role = (int)value; }
            }
    
            public virtual String Designation
            {
                get;
                set;
            }
    
            public virtual String Specialization
            {
                get;
                set;
            }
    
            public virtual ICollection<String> Trainings
            {
                get;
                set;
            }
    
            public virtual ICollection<String> Certifications
            {
                get;
                set;
            }
    
            public virtual Int32? WorkingHours
            {
                get;
                set;
            }
        }





    • Edited by chromeFor Monday, October 28, 2013 3:03 AM
    Monday, October 28, 2013 2:56 AM

All replies

  • These were all my entities, still if something is confusing then please get my all entities from here.

    Now i have enabled migration for this database.

    Also guide me about how to use Person class now?

    I wanted to set it an abstract class but there are some confusions in my mind now.

    Should Person class be a db table or not?

    But when i am trying to use

    Update-Database -verbose

    This command throws an exception.

    guide please. thanks.

    see the below exception and help me to refine with respect to EF.

    PM> Update-Database -verbose
    Using StartUp project 'SchoolManagementSystem'.
    Using NuGet project 'SchoolManagementSystem'.
    Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
    System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

    \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Student' has no key defined. Define the key for this EntityType.
    \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Students' is based on type 'Student' that has no keys defined.

       at System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
       at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
       at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
       at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
       at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
       at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
       at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
       at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
       at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
    One or more validation errors were detected during model generation:

    \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Student' has no key defined. Define the key for this EntityType.
    \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Students' is based on type 'Student' that has no keys defined.


    • Edited by chromeFor Monday, October 28, 2013 3:13 AM Complete example
    Monday, October 28, 2013 3:07 AM
  • Thanks

    Avatar of 1999_ADD_1_Equal_What

    1999_ADD_1_Equal_What

    Its different from my scenario.

    Could you fix this error . thanks in advance . i shared the whole entities . you just need to diagnose the problem and fix them.

    Also guide me about how to use Person class now?

    I wanted to set it an abstract class but there are some confusions in my mind now.

    Should Person class be a db table or not?

    It would be really appreciated if you can do so.


    • Edited by chromeFor Thursday, October 31, 2013 3:45 AM complete
    Thursday, October 31, 2013 3:43 AM