0

I have a table in my EF model that is not creating within the database on access.

The table name is "sites" and is linked to a parent table by the use of a foreign key called "CompanyId".

I am attmempting to list all the sites that belong to a specific company (the company table is linked to the ApplicationUser)

I would expect that when i call the database for a list of the sites that it will be created. However i just receive an error that states the table does not exist

My model for the two tables is as follow

public class Company
{
    [Key]
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string PostCode { get; set; }
    public string County { get; set; }
    public string Country { get; set; }
    public string TelephoneNumber { get; set; }
    public string Email { get; set; }

    public virtual IQueryable<Site> Sites { get; set; }


}



public class Site
{
    [Key]
    public int SiteId { get; set; }
    public int CompanyId { get; set; }
    public string SiteName { get; set; }
    public string Url { get; set; }


}

and my controller

    public ActionResult Index()
    {

        //get the user details
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentuser = manager.FindById(User.Identity.GetUserId());

        // search 
        var company = currentuser.Companies;
        var sites = company.Sites;

        if (sites == null)
        {

           sites = db.Sites.Where(c => c.CompanyId == company.CompanyId);



            return View(sites.ToList());
        }

        return View(company.Sites.ToList());
    }
2
  • Did you add a DbSet for that table in the context? Commented Jun 28, 2017 at 16:56
  • yes i added the DbSet within the DbContext Class Commented Jun 28, 2017 at 17:03

2 Answers 2

1

You can see which table is created in your database using Database Manager?

Try

1: Change

public virtual IQueryable<Site> Sites { get; set; }

for

public virtual ICollection<Site> Sites { get; set; }

2: Add property to your site class

public Company Company { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

this turned out to enable the table to be created, the new still wont load but ill create another question for this. Thanks again
0

Did you create a migration for the change? If not the issue the "Add-Migration whateverYouWant" command in your package manager console.

Do you have automatic migrations enabled? If not then you have to auth the command "update-database" in your package manager console.

Look into your Configuration.cs file under the Migrations folder and check the Configuration constructor. There should be a property called "AutomaticMigrationsEnabled" set to true in order for this to do what you are expecting.

2 Comments

I had enable migrations but not the automatic flag was set to false. I have just tried "add-migration" (which generated a blank migration file) and then "update-database" however there was no change
i have even tried deleting the database but that didnt work

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.