• mvc 连接数据库但单复值得问题


    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为实体类指定映射的表名

    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 }

    方法二:重写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; }
    }


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

    解决方法 : 
       Global.asax  里添加  Database.SetInitializer<_001.Models.MovieDBContext>(null);

      protected void Application_Start()
            {
                Database.SetInitializer<_001.Models.MovieDBContext>(null);
  • 相关阅读:
    SQL Server数据库中批量导入数据
    SQL里面也能用Split()
    MSSQL发送邮件
    Asp.Net简单的发邮件功能
    十大措施保证系统安全性
    WebDriver测试web中遇到的弹出框或不确定的页面
    WebDriver数据驱动模式
    nginx 优化(转)
    配置虚拟目录出错
    14152学期校内岗招聘信息
  • 原文地址:https://www.cnblogs.com/999c/p/6514744.html
Copyright © 2020-2023  润新知