这已经在其他多个问题中得到了解决,但是,我已经尝试了那里发布的所有解决方案都没有成功。我正在使用Code-First EF开发ASP.NET MVC应用程序。我正在尝试利用内置的脚手架,以便它可以根据我的Model和DbContext自动为我创建一个Controller。但是,当我尝试以这种方式创建Controller时,我收到以下错误:
'Unable to retrieve metadata for Employer.' Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used.
我的模型,雇主,我的DbContext,MyDataContext和我的web.config文件的代码如下:
//Employer.cs
public class Employer : Organization, IEmployer
{
public virtual PhysicalAddress Address { get; set; }
public virtual ICollection<ContactMethod> ContactMethods { get; set; }
public int FederalTaxID { get; set; }
public virtual Client Client { get; set; }
}
-
//MyDataContext.cs
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyDataContext : IdentityDbContext<ApplicationUser>
{
public MyDataContext()
: base("DataConnection")
{
}
static MyDataContext()
{
Database.SetInitializer(new DataInitializer());
}
public DbSet<Client> Clients { get; set; }
public DbSet<Organization> Organizations { get; set; } // Store Employer and OrgEntity
/// <summary>
/// Sets up unclear relationships between entities before the models are constructued in a database.
/// For example, models which extend other models must have their Ids mapped (because it is an
/// inherited member, and so is not found by Entity Framework by default)
/// </summary>
/// <param name="modelBuilder">the object responsible for constructing the database from code-first</param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//set up parent organization relationship
modelBuilder.Entity<Organization>()
.HasOptional(n => n.ParentOrganization)
.WithMany(o => o.ChildOrganizations)
.Map(m => m.MapKey("ParentOrganization_Id"));
//set up the organization-employee assignment
modelBuilder.Entity<Employee>()
.HasOptional(a => a.OrganizationAssignment)
.WithMany(a => a.Employees);
//used to integrate Identity stuff into same db as Clients, etc.
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
}
-
<!--web.config-->
.
.
.
<connectionStrings>
<add name="DataConnection"
providerName="Mysql.Data.MysqlClient"
connectionString="server=localhost;user id=myid;password=mypass;persistsecurityinfo=True;database=app_db"/>
</connectionStrings>
.
.
.
来自 this one , this one 和 this one 等其他帖子。我尝试过很多不同的东西让它发挥作用。这包括注释掉MyDbContext
的构造函数,将连接字符串的providerName
属性更改为"System.Data.SqlClient"
,将连接字符串名称更改为"DefaultConnection"
,删除我的连接字符串完全,以及所有这些事情的组合。我确保在尝试添加Controller之间重建。但是,在执行这些不同的更改后,当我尝试添加新的Controller时,仍然会收到相同的错误。
一般来说,我可以找到已经回答的所有问题,但答案对我来说似乎并不合适。我认为将我的案例与链接的案例区分开来的是我的DbContext
实际上是IdentityDbContext
的一个实例。我相信IdentityDbContext
的构造函数只是调用它的基础构造函数,所以我不知道这可能是一个多大的问题。
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
您没有将您的雇主实体映射到模型。这就是编译器无法为Employer检索元数据的原因。首先,如下所示添加DbSet,如果有效,请告诉我。
public DbSet<Client> Clients { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<Employer> Employers { get; set; }