小米小米的type c降噪耳机机怎么样

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
&System.Data.E&&System.Collections.G&&ponentModel.DataA&&System.Data.Entity.I&&&MigrationsDemo&{&&&&&&&BlogContext&:&DbContext&&&&&{&&&&&&&&&&DbSet&Blog&&Blogs&{&&&}&&&&&}&&&&&&&&Blog&&&&&{&&&&&&&&&&&BlogId&{&&&}&&&&&&&&&&&Name&{&&&}&&&&&}&}Now that we have a model it’s time to use it to perform data access. Update the&Program.cs&file with the code shown below.&S&&System.Collections.G&&System.L&&System.T&&&MigrationsDemo&{&&&&&&Program&&&&&{&&&&&&&&&&&Main([]&args)&&&&&&&&&{&&&&&&&&&&&&&&(var&db&=&&BlogContext())&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&db.Blogs.Add(&Blog&{&Name&=&"Another&Blog&"&});&&&&&&&&&&&&&&&&&db.SaveChanges();&&&&&&&&&&&&&&&&&&&(var&blog&&db.Blogs)&&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&Console.WriteLine(blog.Name);&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&&&&&&Console.WriteLine("Press&any&key&to&exit...");&&&&&&&&&&&&&Console.ReadKey();&&&&&&&&&}&&&&&}&}Run your application and you will see that a&MigrationsCodeDemo.BlogContext&database is created for you.&If SQL Express is installed (included in Visual Studio 2010) then the database is created on your local SQL Express instance (.\SQLEXPRESS). If SQL Express is not installed then Code First will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012.Note:&SQL Express will always get precedence if it is installed, even if you are using Visual Studio 2012(LocaDb Database)(SQL Express Database)&Enabling MigrationsIt’s time to make some more changes to our model.Let’s introduce a Url property to the Blog class.&&Url&{&&&}If you were to run the application again you would get an InvalidOperationException stating&The model backing the 'BlogContext' context has changed since the database was created. Consider using Code First Migrations to update the database (&).As the exception suggests, it’s time to start using Code First Migrations. The first step is to enable migrations for our context.Run the&Enable-Migrations&command in Package Manager ConsoleThis command has added a&Migrations&folder to our project, this new folder contains two files:The Configuration class.&This class allows you to configure how Migrations behaves for your context. For this walkthrough we will just use the default configuration.Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type this configuration applies to.An InitialCreate migration. This migration was generated because we already had Code First create a database for us, before we enabled migrations. The code in this scaffolded migration represents the objects that have already been created in the database. In our case that is the&Blog&table with a&BlogId&and&Namecolumns. The filename includes a timestamp to help with ordering.If the database had not already been created this InitialCreate migration would not have been added to the project. Instead, the first time we call Add-Migration the code to create these tables would be scaffolded to a new migration.Multiple Models Targeting the Same DatabaseWhen using versions prior to EF6, only one Code First model could be used to generate/manage the schema of a database. This is the result of a single__MigrationsHistory&table per database with no way to identify which entries belong to which model.Starting with EF6, the&Configuration&class includes a&ContextKey&property. This acts as a unique identifier for each Code First model. A corresponding column in the&__MigrationsHistory&table allows entries from multiple models to share the table. By default, this property is set to the fully qualified name of your context.&Generating & Running MigrationsCode First Migrations has two primary commands that you are going to become familiar with.Add-Migration&will scaffold the next migration based on changes you have made to your model since the last migration was createdUpdate-Database&will apply any pending migrations to the databaseWe need to scaffold a migration to take care of the new Url property we have added. The&Add-Migration&command allows us to give these migrations a name, let’s just call ours&AddBlogUrl.Run the&Add-Migration AddBlogUrl&command in Package Manager ConsoleIn the&Migrations&folder we now have a new&AddBlogUrl&migration. The migration filename is pre-fixed with a timestamp to help with ordering&MigrationsDemo.Migrations&{&&&&&&S&&&&&&System.Data.Entity.M&&&&&&&&&&&partial&&AddBlogUrl&:&DbMigration&&&&&{&&&&&&&&&&&&Up()&&&&&&&&&{&&&&&&&&&&&&&AddColumn("dbo.Blogs",&"Url",&c&=&&c.String());&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&Down()&&&&&&&&&{&&&&&&&&&&&&&DropColumn("dbo.Blogs",&"Url");&&&&&&&&&}&&&&&}&}We could now edit or add to this migration but everything looks pretty good. Let’s use&Update-Database&to apply this migration to the database.Run the&Update-Database&command in Package Manager ConsoleCode First Migrations will compare the migrations in our&Migrations&folder with the ones that have been applied to the database. It will see that theAddBlogUrl&migration needs to be applied, and run it.The&MigrationsDemo.BlogContext&database is now updated to include the&Url&column in the&Blogs&table.&Customizing MigrationsSo far we’ve generated and run a migration without making any changes. Now let’s look at editing the code that gets generated by default.It’s time to make some more changes to our model, let’s add a new&Rating&property to the&Blog&class&&Rating&{&&&}Let's also add a new&Post&class&&Post&{&&&&&&&PostId&{&&&}&&&&&[MaxLength(200)]&&&&&&&Title&{&&&}&&&&&&&Content&{&&&}&&&&&&&&BlogId&{&&&}&&&&&&Blog&Blog&{&&&}&}We'll also add a&Posts&collection to the&Blog&class to form the other end of the relationship between&Blog&and&Post&&List&Post&&Posts&{&&&}We'll use the&Add-Migration&command to let Code First Migrations scaffold its best guess at the migration for us. We’re going to call this migration&AddPostClass.Run the&Add-Migration AddPostClass&command in Package Manager Console.Code First Migrations did a pretty good job of scaffolding these changes, but there are some things we might want to change:First up, let’s add a unique index to&Posts.Title&column&(Adding in line 22 & 29 in the code below).We’re also adding a non-nullable&Blogs.Rating&column. If there is any existing data in the table it will get assigned the CLR default of the data type for new column (Rating is integer, so that would be&0). But we want to specify a default value of&3&so that existing rows in the&Blogs&table will start with a decent rating.(You can see the default value specified on line 24 of the code below)&MigrationsDemo.Migrations&{&&&&&&S&&&&&&System.Data.Entity.M&&&&&&&&&&&partial&&AddPostClass&:&DbMigration&&&&&{&&&&&&&&&&&&Up()&&&&&&&&&{&&&&&&&&&&&&&CreateTable(&&&&&&&&&&&&&&&&&"dbo.Posts",&&&&&&&&&&&&&&&&&c&=&&&&&&&&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&&&&&PostId&=&c.Int(nullable:&,&identity:&),&&&&&&&&&&&&&&&&&&&&&&&&&Title&=&c.String(maxLength:&200),&&&&&&&&&&&&&&&&&&&&&&&&&Content&=&c.String(),&&&&&&&&&&&&&&&&&&&&&&&&&BlogId&=&c.Int(nullable:&),&&&&&&&&&&&&&&&&&&&&&})&&&&&&&&&&&&&&&&&.PrimaryKey(t&=&&t.PostId)&&&&&&&&&&&&&&&&&.ForeignKey("dbo.Blogs",&t&=&&t.BlogId,&cascadeDelete:&)&&&&&&&&&&&&&&&&&.Index(t&=&&t.BlogId)&&&&&&&&&&&&&&&&&.Index(p&=&&p.Title,&unique:&);&&&&&&&&&&&&&&AddColumn("dbo.Blogs",&"Rating",&c&=&&c.Int(nullable:&,&defaultValue:&3));&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&Down()&&&&&&&&&{&&&&&&&&&&&&&DropIndex("dbo.Posts",&[]&{&"Title"&});&&&&&&&&&&&&&DropIndex("dbo.Posts",&[]&{&"BlogId"&});&&&&&&&&&&&&&DropForeignKey("dbo.Posts",&"BlogId",&"dbo.Blogs");&&&&&&&&&&&&&DropColumn("dbo.Blogs",&"Rating");&&&&&&&&&&&&&DropTable("dbo.Posts");&&&&&&&&&}&&&&&}&}Our edited migration is ready to go, so let’s use&Update-Database&to bring the database up-to-date. This time let’s specify the&–Verbose&flag so that you can see the SQL that Code First Migrations is running.Run the&Update-Database –Verbose&command in Package Manager Console.&Data Motion / Custom SQLSo far we have looked at migration operations that don’t change or move any data, now let’s look at something that needs to move some data around. There is no native support for data motion yet, but we can run some arbitrary SQL commands at any point in our script.Let’s add a&Post.Abstract&property to our model. Later, we’re going to pre-populate the&Abstract&for existing posts using some text from the start of theContent&column.&&Abstract&{&&&}We'll use the&Add-Migration&command to let Code First Migrations scaffold its best guess at the migration for us.Run the&Add-Migration AddPostAbstract&command in Package Manager Console.The generated migration takes care of the schema changes but we also want to pre-populate the&Abstract&column using the first 100 characters of content for each post. We can do this by dropping down to SQL and running an&UPDATE&statement after the column is added.(Adding in line 12 in the code below)&MigrationsDemo.Migrations&{&&&&&&S&&&&&&System.Data.Entity.M&&&&&&&&&&&partial&&AddPostAbstract&:&DbMigration&&&&&{&&&&&&&&&&&&Up()&&&&&&&&&{&&&&&&&&&&&&&AddColumn("dbo.Posts",&"Abstract",&c&=&&c.String());&&&&&&&&&&&&&&Sql("UPDATE&dbo.Posts&SET&Abstract&=&LEFT(Content,&100)&WHERE&Abstract&IS&NULL");&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&Down()&&&&&&&&&{&&&&&&&&&&&&&DropColumn("dbo.Posts",&"Abstract");&&&&&&&&&}&&&&&}&}Our edited migration looks good, so let’s use&Update-Database&to bring the database up-to-date. We’ll specify the&–Verbose&flag so that we can see the SQL being run against the database.Run the&Update-Database –Verbose&command in Package Manager Console.&Migrate to a Specific Version (Including Downgrade)So far we have always upgraded to the latest migration, but there may be times when you want upgrade/downgrade to a specific migration.Let’s say we want to migrate our database to the state it was in after running our&AddBlogUrl&migration. We can use the&–TargetMigration&switch to downgrade to this migration.Run the&Update-Database –TargetMigration: AddBlogUrl&command in Package Manager Console.This command will run the Down script for our&AddBlogAbstract&and&AddPostClass&migrations.If you want to roll all the way back to an empty database then you can use the&Update-Database –TargetMigration: $InitialDatabase&command.&Getting a SQL ScriptIf another developer wants these changes on their machine they can just sync once we check our changes into source control. Once they have our new migrations they can just run the Update-Database command to have the changes applied locally. However if we want to push these changes out to a test server, and eventually production, we probably want a SQL script we can hand off to our DBA.Run the&Update-Database&command but this time specify the&–Script&flag so that changes are written to a script rather than applied. We’ll also specify a source and target migration to generate the script for. We want a script to go from an empty database ($InitialDatabase) to the latest version (migrationAddPostAbstract).If you don’t specify a target migration, Migrations will use the latest migration as the target. If you don't specify a source migrations, Migrations will use the current state of the database.Run the&Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract&command in Package Manager ConsoleCode First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save.Generating Idempotent Scripts (EF6 onwards)Starting with EF6, if you specify&–SourceMigration $InitialDatabase&then the generated script will be ‘idempotent’. Idempotent scripts can upgrade a database currently at any version to the latest version (or the specified version if you use&–TargetMigration). The generated script includes logic to check the__MigrationsHistory&table and only apply changes that haven't been previously applied.&Automatically Upgrading on Application Startup (MigrateDatabaseToLatestVersion Initializer)If you are deploying your application you may want it to automatically upgrade the database (by applying any pending migrations) when the application launches. You can do this by registering the&MigrateDatabaseToLatestVersion&database initializer. A database initializer simply contains some logic that is used to make sure the database is setup correctly. This logic is run the first time the context is used within the application process (AppDomain).We can update the&Program.cs&file, as shown below, to set the&MigrateDatabaseToLatestVersion&initializer for BlogContext before we use the context (Line 14). Note that you also need to add a using statement for the&System.Data.Entity&namespace (Line 5).When we create an instance of this initializer we need to specify the context type (BlogContext) and the migrations configuration (Configuration) - the migrations configuration is the class that got added to our&Migrations&folder when we enabled Migrations.&S&&System.Collections.G&&System.L&&System.T&&System.Data.E&&MigrationsDemo.M&&&MigrationsDemo&{&&&&&&Program&&&&&{&&&&&&&&&&&Main([]&args)&&&&&&&&&{&&&&&&&&&&&&&Database.SetInitializer(&MigrateDatabaseToLatestVersion&BlogContext,&Configuration&());&&&&&&&&&&&&&&&(var&db&=&&BlogContext())&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&db.Blogs.Add(&Blog&{&Name&=&"Another&Blog&"&});&&&&&&&&&&&&&&&&&db.SaveChanges();&&&&&&&&&&&&&&&&&&&(var&blog&&db.Blogs)&&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&&Console.WriteLine(blog.Name);&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&}&&&&&&&&&&&&&&Console.WriteLine("Press&any&key&to&exit...");&&&&&&&&&&&&&Console.ReadKey();&&&&&&&&&}&&&&&}&}Now whenever our application runs it will first check if the database it is targeting is up-to-date, and apply any pending migrations if it is not.
阅读(1022)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'EF:Code First Migrations',
blogAbstract:'Code First Migrations',
blogTag:'ef,migration,codefirst',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:3,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'-1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}CodeFirstMigrations更新数据库结构 - 推酷
CodeFirstMigrations更新数据库结构
code first起初当修改model后,要持久化至中时,总要把原数据库给删除掉再创建(DropCreateDatabaseIfModelChanges),此时就会产生一个问题,当我们的旧数据库中包含一些数据时,当持久化更新后,原数据将全部丢失,故我们可以引入EF的数据迁移功能来完成。
已安装NuGet
//原model using System.C using System.Collections.G ponentModel.DataA public class Lesson { public int lessonID { } [Required] [MaxLength(50)] public string lessonName { } [Required] public string teacherName { } public virtual UserInfo UserInfo{} } //新model using System.C using System.Collections.G ponentModel.DataA public class Lesson { public int lessonID { } [Required] [MaxLength(50)] public string lessonName { } [Required] [MaxLength(10)] public string teacherName { } public virtual UserInfo UserInfo{} }
注:区别在于,我们给teacherName属性加了一个长度限制。
接下来,我们将开始持久化此model至数据库中(我们现在只是对属性作修改,此时数据库中此字段的长度为nvarchar(max),并不是nvarchar(10))
1:在config中配置数据库连接:
&connectionStrings&
&add name=&TestUsersDB& connectionString=&Integrated Security=SSPI;Persist Security Info=FInitial Catalog=TestUsersDB;Data Source=XCL-PC\SQLEXPRESS& providerName=&System.Data.SqlClient& /&
&/connectionStrings&
2:打开NuGet控制台:
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致}

我要回帖

更多关于 小米降噪耳机type c版 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信