首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

超级蛋疼的 EF 有关问题,请亲们指点

2012-12-30 
超级蛋疼的 EF 问题,请亲们指点 public class DBContext : DbContext{private const string ConnectString

超级蛋疼的 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");


            this.Property(t => t.AccountName).HasColumnName("AccountName");
            this.Property(t => t.Password).HasColumnName("Password");
            this.Property(t => t.LastLoginTime).HasColumnName("LastLoginTime");
            this.Property(t => t.IsPause).HasColumnName("IsPause");
            this.Property(t => t.LastLoginIp).HasColumnName("LastLoginIp");
            this.Property(t => t.RoleId).HasColumnName("RoleId");
            this.Property(t => t.CreateTime).HasColumnName("CreateTime");


        }
    }

以上是使用ef 映射到表的内容,但使用使用时  提示  对象名 'dbo.AdminInfos' 无效。 
不知道是什么原因 请大虾们 指点。
还有最近经常出现 如下两个问题:
The context cannot be used while the model is being created.
连接未关闭。 连接的当前状态为正在连接。
[解决办法]
看着都累,路过帮顶下吧~~~
[解决办法]
运行的时候,一定把数据库资源管理器中的数据连接->[关闭连接]。
再试试
[解决办法]
ToTable("AdminInfos"); 看你贴得代码应该是AdminInfo 没有s

热点排行