[英]Crud Operations in Entity Framework Codefirst in MVC 4 Using MySql Database
To do CRUD operation I am adding controller and datacontext in scaffolding Options i have received this error 为了执行CRUD操作,我在脚手架选项中添加了控制器和数据上下文,但我收到了此错误
unable to retrieve metadata unable to cast object of type system.data.entity.core.objects.objectcontext to 无法检索元数据,无法将类型为system.data.entity.core.objects.objectcontext的对象转换为
This is my Model 这是我的模特
namespace MvcMySql.Models
{
public class Code
{
[Key]
public int StudentID { get; set; }
public string StudentName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string City { get; set; }
}
public class DatabaseContext : DbContext
{
public DatabaseContext()
: base("ConnectionString")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Code>().HasKey(p => p.StudentID);
}
public DbSet<Code> CodeFirsts { get; set; }
}
}
My web Config 我的网页设定
<connectionStrings>
<add name="ConnectionString"connectionString="server=10.10.30.164;uid=demouser;password=welcome;database=demo"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.SqlClient.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF5, Version=5.0.0.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
I cant identify where the error occurs 我无法确定错误发生的位置
I was able to reproduce this issue locally. 我能够在本地重现此问题。 Then in order to solve it I searched "unrecognized element 'providers' entity framework" on Google as it was the error given on the message box. 然后为了解决该问题,我在Google上搜索了“无法识别的元素'providers'实体框架”,因为它是消息框中给出的错误。
The first link suggested me to remove parameter section but it was not present in my web.config file. 第一个链接建议我删除参数部分,但它不在我的web.config文件中。
I reviewed my web.config files from other projects and found that It does not have a <providers>
section in <entityFramework>
. 我查看了其他项目中的web.config文件,发现它在<entityFramework>
中没有<providers>
部分。
In my current web.config I found it is there (due to downgrade to EF5 from EF6). 在我当前的web.config中,我发现它在那里(由于从EF6降级到EF5)。 Providers are introduced from EF6, read here . 提供程序是从EF6引入的,请在此处阅读。
Current web.config has: 当前的web.config具有:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.SqlClient.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
I removed the provider section and build the solution. 我删除了提供程序部分并构建了解决方案。 Then tried adding the controller using scaffolding and bingo... it worked this time... Now my web.config looks like below: 然后尝试使用脚手架和宾果游戏添加控制器...这一次起作用了...现在我的web.config如下所示:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.