建设银行高校缴费网站,所以免费爱做网站,普通网站 seo 多少钱,网站 建设 维护 公司Code First Fluent API#xff0c;使用流畅API来定义模型映射。 同样使用与上文 Database First 模式相同的例子#xff0c;假设需要设计一个零售系统#xff0c;我们先构建一个 Customer 类。 1 public class Customer
2 {
3 public long Id { get; set; }
4 p… Code First Fluent API使用流畅API来定义模型映射。 同样使用与上文 Database First 模式相同的例子假设需要设计一个零售系统我们先构建一个 Customer 类。 1 public class Customer
2 {
3 public long Id { get; set; }
4 public string Name { get; set; }
5 public string Address { get; set; }
6 public string Phone { get; set; }
7 } 这次没有使用属性来指定对应表名称、主键等。 使用代码创建影射 1 public class CustomerMap : EntityTypeConfigurationCustomer2 {3 public CustomerMap()4 {5 // Primary Key6 this.HasKey(t t.Id);7 8 // Properties9 this.Property(t t.Name)
10 .IsRequired()
11 .HasMaxLength(256);
12
13 this.Property(t t.Phone)
14 .IsRequired()
15 .HasMaxLength(256);
16
17 // Table Column Mappings
18 this.ToTable(Customer, STORE);
19 this.Property(t t.Id).HasColumnName(Id);
20 this.Property(t t.Name).HasColumnName(Name);
21 this.Property(t t.Address).HasColumnName(Address);
22 this.Property(t t.Phone).HasColumnName(Phone);
23
24 // Relationships
25 //this.HasRequired(t t.Status)
26 // .WithMany(t t.CustomerStatus)
27 // .HasForeignKey(d d.Status);
28 }
29 } 在上下文对象中覆写OnModelCreating方法来添加影射配置 1 public class RetailEntities : DbContext2 {3 static RetailEntities()4 {5 //Database.SetInitializerRetailEntities(new CreateDatabaseIfNotExistsRetailEntities());6 //Database.SetInitializerRetailEntities(new DropCreateDatabaseAlwaysRetailEntities());7 //Database.SetInitializerRetailEntities(new DropCreateDatabaseIfModelChangesRetailEntities());8 Database.SetInitializerRetailEntities(null);9 }
10
11 public RetailEntities()
12 : base(NameRetailEntities)
13 {
14 }
15
16 public DbSetCustomer Customers { get; set; }
17
18 protected override void OnModelCreating(DbModelBuilder modelBuilder)
19 {
20 modelBuilder.Configurations.Add(new CustomerMap());
21 }
22 } ICustomerRepository接口和实现依然类似 1 public void InsertCustomer(DomainModels.Customer customer)2 {3 using (RETAILContext context new RETAILContext())4 {5 Customer entity Mapper.MapDomainModels.Customer, Customer(customer);6 context.Customers.Add(entity);7 context.SaveChanges();8 9 customer.Id entity.Id;
10 }
11 }
12
13 public void UpdateCustomer(DomainModels.Customer customer)
14 {
15 using (RETAILContext context new RETAILContext())
16 {
17 Customer entity context.Customers.AsQueryable().Single(c c.Id customer.Id);
18
19 entity.Name customer.Name;
20 entity.Address customer.Address;
21 entity.Phone customer.Phone;
22
23 context.SaveChanges();
24 }
25 } 同样的测试代码 1 ICustomerRepository customerRepository new CustomerRepository();2 3 // 增 4 Console.ForegroundColor ConsoleColor.DarkRed;5 6 DomainModels.Customer customer1 new DomainModels.Customer()7 {8 Name Dennis Gao,9 Address Beijing,
10 Phone 18888888888,
11 };
12 customerRepository.InsertCustomer(customer1);
13 Console.WriteLine(customer1); 当然你可能觉得手工写影射代码还是比较繁琐如果已有部分数据表结构希望反向生成代码可使用工具 Entity Framework Power Tools 来生成。 Entity Framework Power Tools 拯救程序员啊。 完整代码和索引 EntityFramework用法探索系列 一DatabaseFirst二CodeFirst三CodeFirst流畅API四Repository和UnitOfWork五引入Unity六静态Repository七线程安全实践八事务处理完整代码下载 本文转自匠心十年博客园博客原文链接http://www.cnblogs.com/gaochundong/archive/2013/06/06/entityframework_usage_code_first_fluent_api.html如需转载请自行联系原作者