MVC中的常见问题

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

Consider using Code First Migrations to update the database

MVC中的常见问题

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

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

MVC中的常见问题

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

 [Table("User")]
public class User
{
[Key]
public int Id { get; set; }
public string Usn { get; set; }
public string Pwd { get; set; }
public DateTime Created { get; set; }
}

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

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; }
}
上一篇:通过flask实现web页面简单的增删改查bootstrap美化版


下一篇:201871010128-杨丽霞《面向对象程序设计(java)》第一周学习总结