Programming EF with Code First (一) - Configuring

简介: - Data Annotations are attributes that you apply directly to theclass or properties that you want to affect.
-

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);

 

 

 

 

目录
相关文章
|
Ubuntu Unix Linux
成功解决ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the
成功解决ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the
成功解决ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the
|
测试技术
Note tool
Sent: Monday, March 23, 2015 2:56 PM https://dewdfgwd:2030/sap/bc/ui5_ui5/sap/znotetool/index.html?sap-client=001&sap-ui-language=EN&sap-ui-appcache=false 把Opportunity,(或者lead,Appointment,task)ID输入,点submit,就能看到下面挂着的note全部的technical information了 后台只能连AG3哈,这个是拿来做单元测试的。 GM6/001 tcode SE80:
Note tool
|
Go
HANA report creation implementation go through
HANA report creation implementation go through
114 0
HANA report creation implementation go through
另一种无法enable ABAP source code tool的原因
另一种无法enable ABAP source code tool的原因
92 0
|
Java Shell PHP
Guidelines for Function Compute Development - Use Fun Local for Local Running and Debugging
Preface The following key concepts are involved in this document: Function Compute: an event-driven service that allows you to focus on writing and .
1775 0
|
前端开发 测试技术 Ruby