首页 > 代码库 > CodeFirst问题集会一

CodeFirst问题集会一

1、同步数据库时这个要注释掉
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]  这句话去掉注释

编译运行程序时要注释掉,不然报错!

/// <summary>
    /// 客户实体
    /// </summary>
    // [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class CustomerEntities : DbContext, IRequest
    {
        static CustomerEntities()
        {
            DbInterception.Add(new EfIntercepter());
        }
        /// <summary>
        /// 仅用于数据迁移
        /// </summary>
        public CustomerEntities()
            : base("server=192.168.99.62;user=user_1;password=123;database=Customer.Service.Db;port=3306;Charset=utf8;")
        {

        }

        public CustomerEntities(DbConnection existingConnection)
        : base(existingConnection, true)
        {

        }
        public string RequestId { get; set; }
        public string FirstRequestId { get; set; }
        public string NodeName { get; set; }
        public int MvcUserId { get; set; }

        public DbSet<TCustomer> TCustomers { get; set; }
        public DbSet<TCustomerBusiness> TCustomerBusinesss { get; set; }
        public DbSet<TCustomerService> TCustomerServices { get; set; }
        public DbSet<TServiceType> TServiceTypes { get; set; }

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

    }

 2、改数据库的话要改文档,不能直接改库
add-miguration  生成模型
Update-Database -Force –TargetMigration: $InitialDatabase 强制删除
Add-Migration -Force 强制重新建立添加库的cs文件

每次改动先运行 add-miguration  再运行  Update-Database

CodeFirst问题集会一