日期:2014-05-20  浏览次数:20744 次

超级蛋疼的 EF 问题,请亲们指点
 public class DBContext : DbContext
    {
        private const string ConnectString = "Name=DBContext";
        static DBContext()
        {
            Database.SetInitializer<DBContext>(null);
        }
        public DBContext()
            : base(ConnectString)
        {
            // this.Configuration.LazyLoadingEnabled = true;
        }
        public DbSet<AdminInfo> AdminInfos { get; set; }   
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new AdminInfoMap());
            base.OnModelCreating(modelBuilder);
        }
    }


 public class AdminInfoMap : EntityTypeConfiguration<AdminInfo>
    {
        public AdminInfoMap()
        {
            this.HasKey(t => t.AccountId); //主键
            this.Property(t => t.AccountName)
                .IsRequired()
                .HasMaxLength(32);
            this.Property(t => t.Password)
                .IsRequired()
                .HasMaxLength(32);
            this.Property(t => t.LastLoginIp)
                .HasMaxLength(32);
            this.Property(t => t.QQ)
                .HasMaxLength(32);
            this.Property(t => t.Email)
                .HasMaxLength(32);
            this.Property(t => t.RoleId)
                .HasMaxLength(32);

            ToTable("AdminInfos");
            //this.ToTable(typeof(AdminInfo).Name);
            this.Property(t => t.AccountId).HasColumnName("AccountId");