There are two methods to change the default configuration in EF. One is to use data annotations (in the namespace system. componentmodel. dataannotations;), directly acting on the attributes of the class; another is the fluent API, which overwrites the default configuration by adding the corresponding configuration class. Now we can use these two to compare and understand the agreed configuration in EF.
Primary Key: Key
Data Annotations: identifies a primary key using the key keyword
[Key]public int DestinationId { get; set; }
Fluent API:
public class BreakAwayContext : DbContext { public DbSet<Destination> Destinations { get; set; } public DbSet<Lodging> Lodgings { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Fluent API modelBuilder.Entity<Destination>().HasKey(d => d.DestinationId); base.OnModelCreating(modelBuilder); } }
Foreign key
Data Annotations:
public int DestinationId { get; set; } [ForeignKey("DestinationId")] public Destination Destination { get; set; }
Note: If the specified column name exists, the destinationid must exist in the class.
Fluent API:
modelBuilder.Entity<Lodging>().HasRequired(p => p.Destination).WithMany(p=>p.Lodgings).HasForeignKey(p => p.DestinationId);
Length
Data Annotations: Set the length of fields in the database through stringlength (length), minlength (minimum length), and maxlength (maximum length.
[MinLength(10),MaxLength(30)] public string Name { get; set; } [StringLength(30)] public string Country { get; set; }
Fluent API: the minimum length is not set.
modelBuilder.Entity<Destination>().Property(p => p.Name).HasMaxLength(30); modelBuilder.Entity<Destination>().Property(p => p.Country).HasMaxLength(30);
Not empty
Data Annotations: It is identified by required. You can also set whether to allow null strings and Display error messages.
[Required] Public String country {Get; set;} [required (errormessage = "Enter description")] Public String description {Get; set ;}
Fluent API:
modelBuilder.Entity<Destination>().Property(p => p.Country).IsRequired();
Data Type
Data Annotations: typename
// Map string to ntext. The default value is nvarchar (max) [column (typename = "ntext")] Public String owner {Get; set ;}
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Owner).HasColumnType("ntext");
Table Name
Data Annotations: Table
[Table("MyLodging")] public class Lodging { public int LodgingId { get; set; } public string Name { get; set; } public string Owner { get; set; } public decimal Price { get; set; } public bool IsResort { get; set; } public Destination Destination { get; set; } }
Fluent API:
modelBuilder.Entity<Lodging>().ToTable("MyLodging");
Column name
Data Annotations: Column
[Column("MyName")]public string Name { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.Name).HasColumnName("MyName");
Self-Growth
If the primary key is of the int type, EF is set to increase by default. However, if it is of the guid type, the settings to be displayed are automatically increased.
Data Annotations: databasegenerated
public class Person { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid SocialId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
Let's look at the data creation script and add one sentence.
ALTER TABLE [dbo].[People] ADD DEFAULT (newid()) FOR [SocialId]
Fluent API:
modelBuilder.Entity<Person>().Property(p => p.SocialId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Ignore column ing
Some attributes in the class, especially the results obtained by calculating or merging columns, do not need to be recorded in the database, so we can configure it to prevent it from being generated in the database.
Data Annotations: notmapped
[NotMapped] public string Name { get { return FirstName + " " + LastName; } }
Fluent API: notmapped
modelBuilder.Entity<Person>().Ignore(p => p.Name);
Ignore table ing
For tables that do not need to be mapped to the database, we can also cancel the ing.
Data Annotations:
[NotMapped] public class Person { [Key] public Guid SocialId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
Fluent API:
modelBuilder.Ignore<Person>();
Timestamp
The timestamp is only valid for attributes with the Data Type of byte [], and only one attribute set as the timestamp can be in a class.
Data Annotations: Timestamp
1541178922 public Byte[] TimeStamp { get; set; }
Fluent API:
modelBuilder.Entity<Lodging>().Property(p => p.TimeStamp).IsRowVersion();
Complex types
Data Annotations: complextype
[ComplexType] public class Address { public string Country { get; set; } public string City { get; set; } }
Fluent API:
modelBuilder.ComplexType<Address>();
For more information about what a complex type is, see: http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864747.html
EF code first Study Notes: Agreed Configuration