user3240928
user3240928

Reputation: 555

Adding a new table to existing database in mvc4

I am having some trouble understand how to add a new table to an existing database in my web application mvc 4. I have added the table to the database, then i added the model. When I build the solution, i get this error: The namespace 'MvcFFL.Models' already contains a definition for 'PlayerDBContext' C:\Webs\MvcFFL\MvcFFL\Models\Player.cs 19

Here is my Abbrviations model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcFFL.Models
{
    public class Abbrivations
    {

        public int ID { get; set; }
        public string Abbrv { get; set; }
        public string Team { get; set; }
    }

    public class PlayerDBContext : DbContext
    {
        public DbSet<Abbrivations> Abbrvs { get; set; }
    }
}

Does the Dbset line need to go into the Player.cs file or is it actually needed? I would like to be able use the table to add data into

Upvotes: 0

Views: 779

Answers (1)

Tim Sneed
Tim Sneed

Reputation: 490

I would restructure how my EF Context was set up. Have your model classes on their own, with no mention of a context. Then have a context file with your DbContext and DbSet of your models.

I don't usually speak in absolutes, but this is objectively a better way to structure your ef than the way you are currently doing it.

public class PlayerDBContext : DbContext
{
    public DbSet<Abbreviations> Abbrvs { get; set; }
    public DbSet<Team> Teams{ get; set; }
    //and any other context specific information OnModelCreating, SaveChanges, etc
}

Your application context should only be declared once per application, otherwise it will be extermely confusing and more likely, not work.

Upvotes: 1

Related Questions