using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks;namespace EF.Entity {publicabstractclass BaseEntity{////// ID///publicint ID { get; set; }////// 添加时间////// [DataType(DataType.Date)][DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]public DateTime AddedDate { get; set; }////// 修改时间////// [DataType(DataType.Date)][DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]public DateTime ModifiedDate { get; set; }} }
Book实体:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace EF.Entity {////// Book实体///publicclass Book:BaseEntity{////// 书名///publicstring BookName { get; set; }////// 书的作者///publicstring BookAuthor { get; set; }////// 书的价格///publicdecimal BookPrice { get; set; }////// 出版商编号///publicint PublisherId { get; set; }////// 导航属性---出版商///publicvirtual Publisher Publisher { get; set; }} }
出版商实体:
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace EF.Entity {publicclass Publisher:BaseEntity{////// 出版商的名字///publicstring PublisherName { get; set; }////// 导航属性///publicvirtual ICollection Books { get; set; }} }
然后在EF.Data项目中:
BookMap类:
using EF.Entity; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Text; using System.Threading.Tasks;namespace EF.Data {publicclass BookMap:EntityTypeConfiguration{public BookMap(){//配置主键this.HasKey(s => s.ID);//配置字段this.Property(s => s.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);this.Property(s => s.BookName).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();// this.Property(s => s.BookAuthor).HasColumnType("nvarchar(50)").IsRequired();//注意这个和BookName字段配置的区别之处:这样写EF生成不了数据库this.Property(s => s.BookAuthor).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();this.Property(s => s.BookPrice).IsRequired();this.Property(s => s.AddedDate).IsRequired();this.Property(s => s.ModifiedDate).IsRequired();this.Property(s => s.PublisherId).IsRequired();//配置关系[一个出版商可以出版很多书籍]【外键单独配置,不是必须在Property中配置,当然也可以在Property中配置】this.HasRequired(s => s.Publisher).WithMany(s => s.Books).HasForeignKey(s => s.PublisherId).WillCascadeOnDelete(true);//配置表名字this.ToTable("Books");}} }
PublisherMap类:
using EF.Entity; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Text; using System.Threading.Tasks;namespace EF.Data {publicclass PublisherMap:EntityTypeConfiguration{public PublisherMap(){//配置主键this.HasKey(s => s.ID);this.Property(s => s.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);// this.Property(s => s.PublisherName).HasColumnType("nvarchar(50)").IsRequired();//这样写,有问题,生成不了数据库this.Property(s => s.PublisherName).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();this.Property(s => s.AddedDate).IsRequired();this.Property(s => s.ModifiedDate).IsRequired();}} }
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; using System.Web.Mvc;namespace EF.Web.Models {publicclass PublisherModel{public PublisherModel(){PublisherList = new List();}[Display(Name="PublisherName")]publicint PublisherID { get; set; }public List PublisherList { get; set; }} }
数据上下文类:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks;namespace EF.Data {publicclass EFDbContext:DbContext{public EFDbContext(): base("name&#61;DbConnectionString"){ }protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder){var typesToRegister &#61; Assembly.GetExecutingAssembly().GetTypes().Where(type &#61;> !String.IsNullOrEmpty(type.Namespace)).Where(type &#61;> type.BaseType !&#61; null && type.BaseType.IsGenericType&& type.BaseType.GetGenericTypeDefinition() &#61;&#61; typeof(EntityTypeConfiguration<>));foreach (var type in typesToRegister){dynamic configurationInstance &#61; Activator.CreateInstance(type);modelBuilder.Configurations.Add(configurationInstance);} //base.OnModelCreating(modelBuilder); }} }