I. DataAnnotations this method is used to add features to the attributes of an object for operation control. These features are available in usingSystem. componentModel. in DataAnnotations, you must reference the DLLusingSystem. componentModel. dataAnnotations; namespaceModelLib {publicclassCar {[Key] publi
I. Data Annotations this method is used to add features to the attributes of an object for operation control. These features are available in the using System. componentModel. in DataAnnotations, you must reference the DLL using System. componentModel. dataAnnotations; namespace ModelLib {public class Car {[Key] publi
I. Data Annotations
This method controls operations by adding features to the attributes of an object. These features are referenced by DLL under using System. ComponentModel. DataAnnotations.
Using System. componentModel. dataAnnotations; namespace ModelLib {public class Car {[Key] public int ID {get; set;} [Required (ErrorMessage = "cannot be blank")] public string CarNum {get; set;} [StringLength (10, ErrorMessage = "maximum length cannot exceed 10 characters")] public string Colour {get; set;} [Range, errorMessage = "UserYear forensics range: 1-10")] public int UserYear {get; set;} [RegularExpression (@ "^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ ")] public string Email {get; set ;}}}
If the Code does not comply with the above rules, an exception will be reported:
The above just lists a part of the features, other features can be queried MSDN: http://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx
Ii. Fluent API (recommended because the previous range is limited)
1: We can override OnModelCreating In the EF context and perform operations on the attributes to be set. However, if such an object class has three attributes to be configured, if you need to configure 30 object classes for 10 objects, you have to write 30 lines in the OnModelCreating method, which is very troublesome and difficult to maintain. Therefore, it is generally not written in this way;
using System.Data.Entity;using ModelLib;namespace DataLibrary{ public class MyDbContext : DbContext { public MyDbContext() : base("name=MyTestDb") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity
().HasKey(d => d.ID); modelBuilder.Entity
().Property(d => d.Address).IsRequired(); modelBuilder.Entity
().Property(p => p.PassWord).HasMaxLength(50); } public DbSet
Person { get; set; } public DbSet
Home { get; set; } public DbSet
Car { get; set; } }}
2: note that the returned value indicates that the returned value of the Entity <> generic method of modelBuilder is EntityTypeConfiguration <> generic class. We can define a class that inherits from the EntityTypeConfiguration <> generic class to define the database configuration for each class in the domain.
OK. Create a DestinationMap class inherited from EntityTypeConfiguration <> generic class under the DataAccess class library. Write the configuration in the constructor:
using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using ModelLib;namespace DataLibrary{ public class HomeMap:EntityTypeConfiguration
{ public HomeMap() { Property(d => d.Address).IsRequired(); } }}
using System.Data.Entity.ModelConfiguration;using System.Linq;using System.Text;using ModelLib;namespace DataLibrary{ public class PersonMap:EntityTypeConfiguration
{ public PersonMap() { Property(d => d.Age).IsRequired(); } }}
Then modify the EF context:
using System.Data.Entity;using ModelLib;namespace DataLibrary{ public class MyDbContext : DbContext { public MyDbContext() : base("name=MyTestDb") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new HomeMap()); modelBuilder.Configurations.Add(new PersonMap()); } public DbSet
Person { get; set; } public DbSet
Home { get; set; } public DbSet
Car { get; set; } }}
The following are some common settings:
// [Primary Key] // Data Annotations: [Key] public int DestinationId {get; set;} // Fluent API: public class BreakAwayContext: dbContext {protected override void OnModelCreating (DbModelBuilder modelBuilder) {modelBuilder. entity
(). HasKey (d => d. destinationId) ;}/// [foreign key] // Data Annotations: public int DestinationId {get; set;} [ForeignKey ("DestinationId")] public Destination {get; set;} // Fluent API: modelBuilder. entity
(). HasRequired (p => p. destination ). withMany (p => p. lodgings ). hasForeignKey (p => p. destinationId); // [length] // Data Annotations: Use StringLength (length), MinLength (minimum length), MaxLength (maximum length) to set the length of fields in the database [MinLength (10), MaxLength (30)] public string Name {get; set;} [StringLength (30)] public string Country {get; set;} // Fluent API: The method modelBuilder does not set the minimum length. entity
(). Property (p => p. Name). HasMaxLength (30); modelBuilder. Entity
(). Property (p => p. country ). hasMaxLength (30); // [not blank] // Data Annotations: [Required (ErrorMessage = "Enter Description")] public string Description {get; set ;} // Fluent API: modelBuilder. entity
(). Property (p => p. country ). isRequired (); // [Data type] Data Annotations: maps string to ntext. The default value is nvarchar (max) [Column (TypeName = "ntext")]. public string Owner {get; set;} // Fluent API: modelBuilder. entity
(). Property (p => p. owner ). hasColumnType ("ntext"); // [Table name] // Data Annotations: [Table ("MyLodging")] public class Lodging {} // Fluent APImodelBuilder. entity
(). ToTable ("MyLodging"); // [Column Name] // Data Annotations: [Column ("MyName")] public string Name {get; set;} // Fluent API: modelBuilder. entity
(). Property (p => p. name ). hasColumnName ("MyName"); // [auto-increment] // Data Annotations [Key, DatabaseGenerated (DatabaseGeneratedOption. identity)] Guid-type primary key, self-increasing public Guid SocialId {get; set;} // Fluent API: modelBuilder. entity
(). Property (p => p. socialId ). hasDatabaseGeneratedOption (DatabaseGeneratedOption. identity); // [ignore column ing] // Data Annotations: [NotMapped] public string Name {get {return FirstName + "" + LastName ;}// Fluent API: modelBuilder. entity
(). Ignore (p => p. name); // [ignore table ing] // Data Annotations: [NotMapped] public class Person {}// Fluent API: modelBuilder. ignore
(); // [TimeStamp] // Data Annotations: timestamp1421398336public Byte [] TimeStamp {get; set;} can only be byte type/Fluent API: modelBuilder. Entity
(). Property (p => p. timeStamp ). isRowVersion (); // [complex type] // Data Annotations: [ComplexType] public class Address {public string Country {get; set;} public string City {get; set ;}}// Fluent API: modelBuilder. complexType ();