0

I created a model class, ClassOne, for my database as follows

public class ClassOne
{
    [Key]
    public int primary { get; set; }
    public string column1 { get; set; }
    public string column2 { get; set; }
    public string column3 { get; set; }

}

Then in a DAL folder I created, i added the following code

public class MyContext : DbContext
{
    public DbSet<ClassOne> ClassOnes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

And I set up my connection string in my web.config

Instead of adding a controller for the model ClassOne, I want to access the data from my project's home controller file, in the index method. So I added the code

private ProjectContext db = new ProjectContext();

Hand my index method return statement is

return(db.ClassOnes.ToList());

However, when I run the code, I get the following error message:

There is already an object named 'ClassOne' in the database.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: There is already an object named 'ClassOne' in the database.

ANd it points to my return statement in the homecontroller.cs file:

return(db.ClassOnes.ToList());

How can I fix this please?

4

0 回答 0