码迷,mamicode.com
首页 > Web开发 > 详细

MVC中的常见问题

时间:2014-10-08 14:43:05      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   数据   

1.  The model backing the ‘MusicStoreDBContext‘ context has changed since the database was created. 

Consider using Code First Migrations to update the database

bubuko.com,布布扣

 

 

Movie这个表是用来记录Model的版本号的,你每次重新生成一次数据库它就会重新给ModelHash列赋一个新值。
和原始数据库就会产生差异.

2. 自动将数据库的表名变复数,导致错误

 bubuko.com,布布扣

方法一:使用TableAttribute为实体类指定映射的表名

bubuko.com,布布扣
1 [Table("User")]
2 public class User
3 {
4     [Key]
5     public int Id { get; set; }
6     public string Usn { get; set; }
7     public string Pwd { get; set; }
8     public DateTime Created { get; set; }
9 }
View Code

方法二:重写OnModelCreating方法不允许将表名变为复数形式

bubuko.com,布布扣
public class UserContext : DbContext
{
    public UserContext()
        : base("Name=SQLServer")
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
    }

    public DbSet<User> User { get; set; }
}
View Code

 

MVC中的常见问题

标签:style   blog   http   color   io   os   使用   ar   数据   

原文地址:http://www.cnblogs.com/qianjin253738234/p/4010783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!