Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Error Number:2705,State:4,Class:16
Column names in each table must be unique. Column name 'IsActive' in table 'dbo.Enrollments' is specified more than once.


l try to update- databas and l got error below

[EDIT]
Removed all the view and ASP.NET code as it has nothing to do with the error.

the models as
C#
namespace StudentEnrollment.Models
{
public class Course
{

[Display(Name = "Course ID")]
public virtual int Id { get; set; }

  
[Display(Name = "Course Title")]
[Required(ErrorMessage = "Enter Title for course")]
[MaxLength(50, ErrorMessage = "Allowable max is 150")]
public virtual string Title { get; set; }

  
[Display(Name = "Description")]
public virtual string Description { get; set; }

  
[Display(Name = "Number of Credits")]
[Required(ErrorMessage = "Enter credits for course")]
[RegularExpression("^[1-4]{1}$", ErrorMessage = "Enter value either 1,2 or 3,4")]
public virtual string Credits { get; set; }
  
}
}

namespace StudentEnrollment.Models
{
public class Enrollment
{
public virtual int Id { get; set; }

[Display(Name = "Student ID")]
public virtual int StudentId { get; set; }
public Student Student { get; set; }

[Display(Name = "Course ID")]
public virtual int CourseId { get; set; }

public Course Course { get; set; }

[Display(Name = "Grade")]
[RegularExpression("^[A-F]{1}$", ErrorMessage = "Enter value A , B, C, D, E, F")]
public virtual string Grade { get; set; }
public bool IsActive { get; set; }

[Display(Name = "Assigned Campus")]
[Required(ErrorMessage = "Enter value for campus")]
public virtual string AssignedCampus { get; set; }

  
[Display(Name = "Enrolled in Semester")]
[Required(ErrorMessage = "Enter enrollment semester")]
public virtual string EnrollmentSemester { get; set; }


[Display(Name = "Enrollment Year")]  
[Required(ErrorMessage = "Enter value for Enrollment Year ")]
[Range(2018,2020, ErrorMessage = "Enter value greater than 2018")]
public virtual int EnrollmentYear { get; set; }
public virtual String Notes { get; set; }
}
}

namespace StudentEnrollment.Models
{
public class Student
{
[Display(Name = "Student ID")]
public virtual int Id { get; set; }

[Display(Name = "Last Name")]
[Required(ErrorMessage = "Enter value for Last Name")]
[MaxLength(50, ErrorMessage = "Allowable max is 50")]
public virtual string LastName { get; set; }


[Display(Name = "First Name")]
[Required(ErrorMessage = "Enter value for First Name")]
[MaxLength(50, ErrorMessage = "Allowable max length is 50")]
public virtual string FirstName { get; set; }
}
}

there is still AN error

at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) ClientConnectionId:5f388873-4c88-4dfa-96ec-5412a7dfdcce Error Number:2705, State:4,Class:16 olumn names in each table must be unique. Column name IsActive in table dbo. Enrollments is specified more than once. PM>

Is there is soltion as query inside the table to solve the issues????

this is the enrollemnt query
SQL
CREATE TABLE [dbo].[Enrollments] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[StudentId] INT NOT NULL,
[CourseId] INT NOT NULL,
[Grade] NVARCHAR (MAX) NULL,
[IsActive] BIT NULL,
[AssignedCampus] VARCHAR (MAX) NULL,
[EnrollmentSemester] VARCHAR (MAX) NULL,
[EnrollmentYear] INT NULL,
CONSTRAINT [PK_dbo.Enrollments] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Enrollments_dbo.Courses_CourseId] FOREIGN KEY ([CourseId]) REFERENCES [dbo].[Courses] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_dbo.Enrollments_dbo.Students_StudentId] FOREIGN KEY ([StudentId]) REFERENCES [dbo].[Students] ([Id]) ON DELETE CASCADE
);


GO
CREATE NONCLUSTERED INDEX [IX_StudentId]
ON [dbo].[Enrollments]([StudentId] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_CourseId]
ON [dbo].[Enrollments]([CourseId] ASC);

Show transcribed image text

What I have tried:

l did try update-database using visual studio 2017 but it keep generating the same error Is active is more than one and also Notes column is not valid
this the error message
Error Number:2705,State:4,Class:16
Column names in each table must be unique. Column name 'IsActive' in table 'dbo.Enrollments' is specified more than once.
PM>
Posted
Updated 14-Oct-18 9:50am
v5
Comments
Richard MacCutchan 12-Oct-18 5:15am    
The error message is perfectly clear; what do you not understand?
ZurdoDev 12-Oct-18 14:25pm    
OP is using Entity Framework which means they did not write the code for INSERT/UPDATE, the framework did.
Richard MacCutchan 12-Oct-18 15:20pm    
But they still did the base design. And it is no good using a framework if you do not understand what is going on inside it.
ZurdoDev 12-Oct-18 14:26pm    
You probably have some code somewhere calling SQL or your model is messed up. Maybe refresh your model?

The problems are with your database Models

I removed all of the attributes for clarity

In your Student model, all of your fields are virtual. That's bad. They shouldn't be because they are not field relating with other fields in other tables. Your Student class should be closer to this:
C#
namespace StudentEnrollment.Models
{
	public class Student
	{
		public int StudentId { get; set; }
		public string LastName { get; set; }
		public string FirstName { get; set; }
	}
}


Your Course model has the same problems. Everything is virtual when it shouldn't be.
C#
namespace StudentEnrollment.Models
{
	public class Course
	{
		public int CourseId { get; set; }
		public string Title { get; set; }
		public string Description { get; set; }
		public string Credits { get; set; }
	}
}


Now, your Enrollment model has a similar problem. Everything that is virtual shouldn't be and the stuff that isn't virtual should be! Plus, you don't need the StudentId and CourseID fields. Those will be generated by EF as appropriate.
C#
namespace StudentEnrollment.Models
{
	public class Enrollment
	{
		public int EnrollmentId { get; set; }
		public string Grade { get; set; }
		public bool IsActive { get; set; }
		public string AssignedCampus { get; set; }
		public string EnrollmentSemester { get; set; }
		public int EnrollmentYear { get; set; }
		public String Notes { get; set; }

		public virtual Student Student { get; set; }
		public virtual Course Course { get; set; }
	}
}
 
Share this answer
 
Thank you for your answer l thought it was virtual
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900