使用MVC3和EF框架后,怎么查询时都会在表名上自动加上S,执行的时候肯定会提示表不存在.怎么解决
不可能去改数据库.头大.
直接写的类,没有产生 edmx 文件.
EF6写法:
引入命名空间:
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Data.Entity.ModelConfiguration.Conventions;
DBContext类重写方法:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
你写的代码有问题。我没发现会自动加上S啦。
我记得edmx属性里面好象有个设置复数的
是Code First不?
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Example
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.ComponentModel.DataAnnotations;
public partial class ExampleDbContext : DbContext
{
public AssessmentSysDbContext()
: base("name=ConnStringKey")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// TODO: 在这里写Fluent API
#region 公约
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
#endregion
//....
modelBuilder.Entity<Vote>()
.Ignore(v => v.IsSelected);
#endregion
}
//....
}
}
考虑用EdmGen2命令直接生成 edmx文件,EdmGen2 /ModelGen "DATA SOURCE=ORAFH;PERSIST SECURITY INFO=True;USER ID=FHOPE;PASSWORD=fhope" "Oracle.DataAccess.Client" "TeleMed"
在Model里的class上面加[Table("Test")],一句话解决才是王道
和楼主说的一样,并没有生成demx,直接是代码,我也有这个现象,在Class上面加 tablename也不行!!!怎么解决?
直接在Model里的class上面加[Table("表名")]