国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

由code生成數據庫model

2019-11-17 02:34:44
字體:
來源:轉載
供稿:網友

由code生成數據庫model

在做項目的時候,用到了entity framework,數據庫是MySQL。以前一直是先建好DB model,然后項目中添加。但是之后改動db,又需要update。像同事學習了:code make db model。

需要的dll:MySql.data(6.9.5.0), MySql.data.Entity.EF6(6.9.5.0),

System.Data, System.Sata.DataSetExtension, System.Data.SQLite,System.Data.SQLite.EF6,System.Data.SQLite.Linq, EntityFramework, EntityFramework.SqlServer

config文件的連接字符串:

<connectionStrings> <add name="JobMasterDBConnection_MSSQL" connectionString="Data Source=EISCNG109WQS1;Initial Catalog=JobMaster;Integrated Security=True" Word!01;" providerName="MySql.Data.MySqlClient" /> <add name="JobMasterDBConnection_SQLite" connectionString="Data Source=DB/JobMaster.local.db;" providerName="System.Data.SQLite.EF6" /> </connectionStrings>

Context和Migrations文件:

 1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Data.Entity; 6 using System.Data.Entity.Migrations; 7 using System.Data.Entity.ModelConfiguration; 8 using System.Data.Entity.ModelConfiguration.Conventions; 9 using System.Linq;10 using System.Text;11 12 namespace JobMaster.Libs.Models.DBContext13 {14     //[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]15     internal class JobMasterDBContext : DbContext16     {17         #region "Fields"18         public const string CNSTR_CONNECTIONNAME = "JobDispatcher_Database_Entry";19         public const string CNSTR_CONNECTIONNAME_MSSQL = "JobMasterDBConnection_MSSQL";20         public const string CNSTR_CONNECTIONNAME_MYSQL = "JobMasterDBConnection_MYSQL";21         public const string CNSTR_CONNECTIONNAME_SQLITE = "JobMasterDBConnection_SQLite";22         #endregion23 24         #region "Constructs"25         public JobMasterDBContext()26             : this(ConfigurationManager.AppSettings[CNSTR_CONNECTIONNAME])27         { }28 29         public JobMasterDBContext(string connectionName)30             : base(connectionName)31         {32             this.Configuration.LazyLoadingEnabled = false;33             this.Configuration.ProxyCreationEnabled = false;34         }35 36         static JobMasterDBContext()37         {38             switch (ConfigurationManager.AppSettings[CNSTR_CONNECTIONNAME])39             {40                 case CNSTR_CONNECTIONNAME_MSSQL:41                     break;42                 case CNSTR_CONNECTIONNAME_MYSQL:43                     DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());44                     Database.SetInitializer<JobMasterDBContext>(new MigrateDatabaseToLatestVersion<JobMasterDBContext, JobMaster.Libs.Models.Migrations.JobMasterDBConfiguration>());45                     break;46                 case CNSTR_CONNECTIONNAME_SQLITE:47                     //Database.SetInitializer<JobMasterDBContext>(new DropCreateDatabaseAlways<JobMasterDBContext>());48                     break;49             }50         }51         #endregion52 53         #region "Propertiess"54         public DbSet<Agent> Agents { get; set; }55 56         public DbSet<Job> Jobs { get; set; }57 58         public DbSet<TestCase> TestCases { get; set; }59 60         public DbSet<Task> Tasks { get; set; }61 62         public DbSet<TaskAssignment> TaskAssignments { get; set; }63         #endregion64 65         #region "Events"66         protected override void OnModelCreating(DbModelBuilder modelBuilder)67         {68             modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();69             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();70 71             //modelBuilder.Entity<EntityType>().MapToStoredProcedures();72 73             modelBuilder.Entity<TestCase>().Property(p => p.TaskID).IsOptional();74             modelBuilder.Entity<Task>().HasMany(p => p.TestCases).WithOptional().HasForeignKey(p => p.TaskID);75         }76         #endregion77     }78 }
namespace JobMaster.Libs.Models.Migrations{    using System;    using System.Data.Entity;    using System.Data.Entity.Migrations;    using System.Linq;    internal sealed class JobMasterDBConfiguration : DbMigrationsConfiguration<JobMaster.Libs.Models.DBContext.JobMasterDBContext>    {        public JobMasterDBConfiguration()        {            AutomaticMigrationsEnabled = true;            switch (System.Configuration.ConfigurationManager.AppSettings[JobMaster.Libs.Models.DBContext.JobMasterDBContext.CNSTR_CONNECTIONNAME])            {                case JobMaster.Libs.Models.DBContext.JobMasterDBContext.CNSTR_CONNECTIONNAME_MSSQL:                    break;                case JobMaster.Libs.Models.DBContext.JobMasterDBContext.CNSTR_CONNECTIONNAME_MYSQL:                    SetSqlGenerator("MySql.Data.MySqlClient.EF6", new MySql.Data.Entity.MySqlMigrationSqlGenerator());                    break;                case JobMaster.Libs.Models.DBContext.JobMasterDBContext.CNSTR_CONNECTIONNAME_SQLITE:                    break;            }        }        protected override void Seed(JobMaster.Libs.Models.DBContext.JobMasterDBContext context)        {            //  This method will be called after migrating to the latest version.            //  You can use the DbSet<T>.AddOrUpdate() helper extension method             //  to avoid creating duplicate seed data. E.g.        }    }}

Model:

using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Runtime.Serialization;using System.Text;using System.Threading.Tasks;using JobMaster.Libs.Models;using JobMaster.Libs.Models.ViewModels;using JobMaster.Libs.Utils;using JobMaster.Libs.Utils.Extensions;namespace JobMaster.Libs.Models{    [Table("tbl_Jobs")]    public class Job    {        #region "Constructs"        public Job() { }        #endregion        #region "Properties"        [Key]        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]        public long ID { get; set; }        [MaxLength(100)]        public string Name { get; set; }        [MaxLength(300)]        public string Description { get; set; }        public Nullable<bool> EnableSliceSet { get; set; }        public Nullable<int> MinialSliceSize { get; set; }        [Required]        [MaxLength(300)]        public string PrepConfig { get; set; }        public Nullable<int> Priority { get; set; }        public Nullable<JobType> JobType { get; set; }        public Nullable<JobStatus> Status { get; set; }        public Nullable<int> TotalCount { get; set; }        public Nullable<int> TotalSuccess { get; set; }        public Nullable<int> TotalFailure { get; set; }        public Nullable<int> TotalTimeout { get; set; }        public Nullable<System.DateTime> CreateTime { get; set; }        public Nullable<System.DateTime> StartTime { get; set; }        public Nullable<System.DateTime> EndTime { get; set; }        public Nullable<System.DateTime> UpdateTime { get; set; }        #endregion        #region "Properties"        public virtual ICollection<TestCase> TestCases { get; set; }        public virtual ICollection<Task> Tasks { get; set; }        #endregion    }}

如何使用:

JobMasterDBContext  dbcontext=new JobMasterDBContext();dbcontext.Jobs.add(new Job(){Name="test"});dbcontext.SaveChanges();

注意:在需要用到db 的項目中也需要添加以上Dll。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景宁| 白河县| 聂荣县| 屏东县| 依安县| 阿拉善左旗| 修文县| 航空| 定西市| 金寨县| 宁河县| 云霄县| 湘阴县| 栖霞市| 中江县| 南昌县| 岢岚县| 堆龙德庆县| 剑阁县| 苏尼特左旗| 平顺县| 琼海市| 化隆| 安陆市| 东乡| 叶城县| 齐齐哈尔市| 台北县| 江山市| 海林市| 金湖县| 长岭县| 宜君县| 临武县| 巢湖市| 灌南县| 阜阳市| 通河县| 体育| 施甸县| 庐江县|