这一章主要主要讲的是我们的模型如何映射到数据库,而不影响模型,以及不同的映射场景。

一、表名和列名

  1.指定表名

[Table("PersonPhotos")]
public class PersonPhoto

 或

[Table("Locations", Schema="baga")]
public class Destination

Schema修改数据库架构,默认是dbo。

API:

modelBuilder.Entity<Destination>().ToTable("Locations", "baga");

2.列名

[Column("LocationID")]
public int DestinationId { get; set; }
[Required, Column("LocationName")]
public string Name { get; set; }

API:

Property(d => d.Nam
.IsRequired().HasColumnName("LocationName");
Property(d => d.DestinationId).HasColumnName("LocationID");

二、表分割

将一个模型拆成两张表,比如Destination。

public class Destination
    {
        [Key]
        public int DestinationId { get; set; }
        [Required]
        public string Name { get; set; }
        public string Country { get; set; }
        [MaxLength(500)]
        public string Description { get; set; }
        [Column(TypeName = "image")]
        public byte[] Photo { get; set; }
        public List<Lodging> Lodgings { get; set; }
    }

API:(DataAnnotations不能处理子对象)

 modelBuilder.Entity<Destination>()
                .Map(m =>
                {
                    m.Properties(d => new {d.Name, d.Country, d.Description});
                    m.ToTable("Locations");
                })
                .Map(m =>
                {
                    m.Properties(d => new {d.Photo});
                    m.ToTable("LocationPhotos");
                });

运行后,Destination 拆分成了Locations和LocationPhotos

Programming Entity Framework CodeFirst--数据库约定和配置

 当Destination添加数据的时候,这个两个表的主键都会增加。

Programming Entity Framework CodeFirst--数据库约定和配置  Programming Entity Framework CodeFirst--数据库约定和配置

三、数据库映射控制

  1.模型要映射到数据库中有三种方式。

   1).将对象加入到Dbset中。
   2).在别的类型中,引用当前类型。(Person包含PersonPhoto,PersonPhoto是不需要加人Dbset的。)
   3).通过API在DbModelBuilder方法中配置。

 前面两种我们前面都已经尝试过,对于第三种,不使用Dbset 就需要使用EntityTypeConfiguration。 可以建立一个空的:

public class ReservationConfiguration :EntityTypeConfiguration<Reservation>
{}

再加入modelBuider

modelBuilder.Configurations.Add(new ReservationConfiguration());

  2.忽略类型映射。

如果不想数据库映射某个类型,我们可以将其忽略掉。

[NotMapped]
public class MyInMemoryOnlyClass
//API:
modelBuilder.Ignore<MyInMemoryOnlyClass>();

  3.属性映射类型

  1)只能是EDM支持的类型。

   Binary, Boolean, Byte, DateTime, DateTimeOffset, Decimal, Double, Guid, Int16, Int32, Int64, SByte, Single, String, Time

  枚举类型现在已经支持了。MyType是个枚举类型,Flag是Uint型,不支持EF就自动忽略了。

 Programming Entity Framework CodeFirst--数据库约定和配置

 2)可获取的属性 

   .Public属性会自动映射。
   .Setter可以是限制访问,但Getter必须是Public。
   .如果想非Public的属性也映射,就需要通过API来配置。

 如果想配置私有属性,这样就需要将Config类置于内部。如下,Name是private的,PersonConfig想要获取这个类型就需要置于Person内部了:

public class Person
{
public int PersonId { get; set; }
private string Name { get; set; }
public class PersonConfig : EntityTypeConfiguration<Person>
{
public PersonConfig()
{
Property(b => b.Name);
}
}
public string GetName()
{
return this.Name;
}
public static Person CreatePerson(string name)
{
return new Person { Name = name };
}
}
View Code

相关文章:

  • 2022-01-20
  • 2022-12-23
  • 2022-01-18
  • 2022-01-24
猜你喜欢
  • 2021-12-13
  • 2022-02-05
  • 2021-12-02
  • 2022-12-23
  • 2021-07-23
  • 2021-10-29
  • 2021-10-06
相关资源
相似解决方案