asp.net unable to retrieve metadata

2019-08-05 16:22发布

问题:

hi i am having a issue with MVC to make a controller even after reading the tips. adding a constructor on the DbContext, deleting the non worked, changing providerName="System.Data.SqlClient" and so on.

my model class look like this:

public class RecruiterModel
{
    public string CompanyName { get; set; }
    public string Website { get; set; }
    public string CompanySize { get; set; }
    public string LinkedInCompanyURL { get; set; }
    public string LinkedInID { get; set; }
    public string Specialities { get; set; }
    public string Category { get; set; }
    public string Location { get; set; }
    public int ContactPhone { get; set; }
    public string ContactEmail { get; set; }
}

public class RecruiterDBContext : DbContext
{

    public DbSet<RecruiterModel> Recruiters { get; set; }
}

and my web.config connectionStrings looks like this:

<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated  Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />
<add name="RecruiterDBContext"
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Recruiters.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient"
/>

any tips?

回答1:

Add the following property to you Recruiter class:

public string Id { get; set; }

Or apply the Key attribute to one of you existing properties. For example:

[Key]
public string CompanyName { get; set; }

on MSDN, you have the Code First Conventions topic:

Primary Key Convention

Code First infers that a property is a primary key if a property on a class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column.

public class Department
{
    // Primary key
    public int DepartmentID { get; set; }

    . . .    

}

Another reference on the topic is made in this Code First DataAnnotations article:

Key

Entity Framework relies on every entity having a key value (aka EntityKey) that it uses for tracking entities. One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. That convention is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. In addition to EF’s use of the EntityKey, the property will map to a primary key column in the database.



回答2:

I have found the solution to create the controller class: On the model class i added the

using System.ComponentModel.DataAnnotations;

public class RecruiterModel { [Key] public string Id { get; set;

then on the web.config i commented out the whole

connectionStrings tag

then i created the controller but and it worked.

thanks gos out to Alex Filipovici and Young Yang - MSFT on: http://forums.asp.net/t/1888573.aspx/1?Unable+to+retrieve+metadata+for+when+trying+to+create+a+controller

though it works but on the Solution Explorer tab under App_Data i do not see the db file... hmm another issue.