???CodeFirst???????????????
????????? ???????[ 2016/7/14 10:22:20 ] ????????????? CodeFirst
?????????????????????CodeFirst??????????????????
?????????Entity Framwork??????????ModelFist??DBFirst??CodeFirst???У?CodeFirst?????д???????????
????????CodeFist????????????????λ?????????????????????????
????1. ???CodeFirst????????????
????????????????????????????????????Model:Author??Blog???DbContext?? DbContext??????????£?
????????????->???->?????->ADO.NET Entity Data Model->Empty Code First model
??????????????£?
1 //????????????????????+???s?????????TableAttribute?????????????T_Authors
2 [Table("T_Authors")]
3 public class Author
4 {
5 public int Id { set; get; }
6 public string Name { set; get; }
7 /*
8 ?????????Blog??????????????????Blog?????????????????????????Σ???public int Id { set; get; }??
?????????????????????"EntityType 'Blog' has no key defined. Define the key for this EntityType.
9 ????????Blogs: EntityType: EntitySet 'Blogs' is based on type 'Blog' that has no keys defined."
10 ????????*/
11 public virtual ICollection<Blog> Blogs { set; get; }
12 }
13
14 [Table("T_Blogs")]
15 public class Blog
16 {
17 public int Id { set; get; }
18 public string Title { set; get; }
19 public DateTime Time { set; get; }
20 public int AuthorId { set; get; }
21 public virtual Author Author { set; get; }
22 }
23
24 public class MyDbContext: DbContext
25 {
26 public MyDbContext()
27 : base("name=MyDbContext")
28 {
29 }
30 //DbContext??????????????connectionStrings?????????????????????????
31 //DbContext????DbSet??????????????????????????????Author???????????????????T_Authors?????
32 public virtual DbSet<Author> Authors { set; get; }
33 }
????CodeFirst????????????????е??????????????????С????????MYSQL???????????????£?
1 <!--EF for MYSQL-->
2 ??<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration?? MySql.Data.Entity.EF6">
3 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory?? EntityFramework" />
4 <providers>
5 <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices?? MySql.Data.Entity.EF6" />
6 </providers>
7 </entityFramework>
8 <connectionStrings>
9 <add name="MyDbContext" connectionString="server=localhost;port=3306;database=EF;uid=root;password=root" providerName="System.Data.MySqlClient" />
10 </connectionStrings>
????PS:С????????EF6??MYSQL?????????????????????Mysql.Data.Entity.EF6???EntityFrameword 6.0?????á?
??????????????????????????????????????????Main??????д???д???
????1 using (var db = new ManagerDb())
????2 {
????3 db.Authors.Add(new Author() { Name = "xfh" });
????4 db.SaveChanges();
????5 }
???????г??????????EF????????????????????????????EF(??????????????)????????T_Authors??
???????????????DbContext???Blog??????????????????????????T_Blogs???????????Author???ж?????Blog????????????????????
????public virtual ICollection<Blog> Blogs { set; get; } ????????????????????????????T_Blogs?????????T_Authors???????????????????_migrationhistory??????????????????????????????????????е?MigrationId??ε??????????????Migration??????????????????????ε???????????????? Update-Database -TargetMigration:MigrationIdValue ?????????????MigrationIdValue???????Migration??????Git?汾?????е???????????????????????????汾?????????????????_migrationhistory?????????????????Migration?????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11