zoukankan      html  css  js  c++  java
  • Programming EF with Code First (一) Configuring

    -

    Data Annotations are attributes that you apply directly to the
    class or properties that you want to affect. These can be found in the System.Component
    Model.DataAnnotations namespace.

    class AnimalType
    {
      public int Id { getset; }
      [Required]
      public string TypeName { getset; }
    }

    when it’s time to SaveChanges, Entity Framework will check to be sure that the property you have
    flagged as Required is not empty. If it is empty, Entity Framework will throw an
    exception.

    [Table("Species")]
    class AnimalType
    {
      public int Id { getset; }
      [Required]
      public string TypeName { getset; }
    }

    The data that you refer to as AnimalType in your application might be stored in a table called Spe
    cies. The Table annotation allows you to specify this mapping.

     上面的配置等价于如下的Fluent API:

    class VetContext:DbContext
    {
      public DbSet<Patient> Patients { getset; }
      public DbSet<Visit> Visits { getset; }
      protected override void OnModelCreating
      (DbModelBuilder modelBuilder)
      {
        modelBuilder.Entity<AnimalType>()
         .ToTable("Species");
        modelBuilder.Entity<AnimalType>()
         .Property(p => p.TypeName).IsRequired();
      }
    }

    Organizing Fluent Configurations

    You can group configuration by entity type within
    individual EntityTypeConfiguration classes, and then tell the DbModelBuilder about
    them in the OnModelCreating method. DbModelBuilder has a Configurations property to
    which you can add these EntityTypeConfigurations.

    using System.Data.Entity.ModelConfiguration;
    using Model;

    public class DestinationConfiguration : EntityTypeConfiguration<Destination>
    {
      public DestinationConfiguration()
      {
        Property(d => d.Name).IsRequired();
        Property(d => d.Description).HasMaxLength(500);
        Property(d => d.Photo).HasColumnType("image");
      }
    }

    public class LodgingConfiguration : EntityTypeConfiguration<Lodging>
    {
      public LodgingConfiguration()
      {
        Property(l => l.Name).IsRequired().HasMaxLength(200);
      }
    }
    //Adding the configuration classes in //OnModelCreating
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      modelBuilder.Configurations.Add(new   DestinationConfiguration());
      modelBuilder.Configurations.Add(new LodgingConfiguration());
    }

     关于主键

    public class Person
    {
     [Key]
     public int SocialSecurityNumber { getset; }
     public string FirstName { getset; }
     public string LastName { getset; }
    }

    如果用下面的代码去进行插入,会报错

    private static void InsertPerson()
    {
     var person = new Person
     {
      FirstName = "Rowan",
      LastName = "Miller",
      SocialSecurityNumber = 12345678
     };
     using (var context = new BreakAwayContext())
     {
      context.People.Add(person);
      context.SaveChanges();
     }
    }

     以为, key属性默认是自增的字段

    必须用如下代码来进行修复

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int SocialSecurityNumber { get; set; }

    Configuring Database-Generated Options with the Fluent API

    modelBuilder.Entity<Trip>()
    .HasKey(t => t.Identifier)
    .Property(t => t.Identifier)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    技术改变世界
  • 相关阅读:
    听说这是互联网时代100本必读书单,你看过几本?
    HttpURL连接远程serverGet和Post方式请求并返回数据
    如何才干高速成为优秀的程序猿
    android学习笔记NO.5
    Linux 内核开发
    scikit-learn:3.3. Model evaluation: quantifying the quality of predictions
    多个client与一个server端通信的问题
    [Android Studio] 取消引用库打包出现异常-- provided dependencies can only be jars
    2016.3.16__CSS3_选择器_边框_背景_蒙版mask__第九天
    Android Studio:Multiple dex files define Landroid/support/annotation/AnimRes
  • 原文地址:https://www.cnblogs.com/davidgu/p/2674926.html
Copyright © 2011-2022 走看看