0

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

3
  • do you by any chance have one more class that is also called PlayerDBContext? Commented May 20, 2014 at 17:49
  • Yes I do, I understand that is the error but why when i have it referencing a different part of the DBset? Commented May 20, 2014 at 17:53
  • Why not just add all DbSets to a single DbContext? Is there any reason why you separated them? Commented May 20, 2014 at 17:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh ok, I can understand that now, thanks that worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.