asp.net mvc project

Nourhan Elmahdy 1 Reputation point
2022-05-10T08:30:38.897+00:00

this error appears two me while I enable-migrations

the Error :

One or more validation errors were detected during model generation:

IDs_Students_Target: : Multiplicity is not valid in Role 'IDs_Students_Target' in relationship 'IDs_Students'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
IDs_Students_Source_IDs_Students_Target: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'City' on entity 'Students' does not match the type of property 'StudentId' on entity 'IDs' in the referential constraint 'IDs_Students'.

my code :

public class Students
{
[Key]
public int StudentId { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "the string length must be 50")]
    [Display(Name = "First Name")]
    public string Fname { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Second Name")]
    public string Lname { get; set; }

    public virtual IDs IDs { get; set; }
}

}

public class IDs
{
[Key]
[ForeignKey("Students")]
public int StudentId { get; set; }

    public string Code { get; set; }
    public string City { get; set; }
    public string Gender { get; set; }

    public virtual Students Students { get; set; }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,408 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,831 Reputation points Microsoft Vendor
    2022-05-11T02:55:12.997+00:00

    Hi @Nourhan Elmahdy ,
    I tried your code and migrated successfully. My migration steps:

    1. Create the model: Students.cs(copy paste your code)
    2. Download NuGet Package: Entity Framework(Click Build Solution)
    3. Add /Controller/MVC 5 Controller with views, using Entity Framework
    4. Open the Package Manager Console. Select Tools > NuGet Package Manager > Package Manager Console, enter the migration command:
    • Enable-Migrations
      Open the Configuration.cs file in the Migrations folder. Locate the class constructor and change the AutomaticMigrationsEnabled value to true.
    • add-migration InitialCreate
    • Update-Database -Script -SourceMigration:$InitialDatabase
    • Update-Database -Verbose

    https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-entity-framework-scaffolding-and-migrations
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments