Loading

随笔分类 -  Entity Framework

摘要:使用code first 做数据迁移新增非空字段,提示如下错误:AlterColumn("dbo.StoreProduct", "StoreProductAttribute", c => c.String(nullable: false, maxLength: 20, defaultValue: "固定资产"));不能将值 NULL 插入列 'ProductAttribute',表 'dbo.StoreProduct';列不允许有空值。UPDATE 失败。语句已终止。由于数据表StoreProdu 阅读全文
posted @ 2013-02-02 15:00 chear 阅读(2261) 评论(0) 推荐(0)
摘要:protected override void Seed(Context context){ CreateIndex(context, "ProductName", "StoreProduct", true);}private void CreateIndex(Context context, string field, string table, bool unique = false) { context.Database.ExecuteSqlCommand(String.Format("CREATE {0}NONCLUSTERED IND 阅读全文
posted @ 2013-01-10 11:06 chear 阅读(3374) 评论(1) 推荐(3)
摘要:public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString)) { IQueryable<ClassInformation> result = from c in db.GetTable<TClass>() ... 阅读全文
posted @ 2012-12-28 10:10 chear 阅读(30041) 评论(0) 推荐(1)
摘要:详情请看:http://stackoverflow.com/questions/12809958/ef-how-do-i-call-savechanges-twice-inside-a-transactionusing (var transaction = new TransactionScope()){ // Do something db.SaveChanges(); // Do something else db.SaveChanges(); tramsaction.Complete();}使用上面代码会报数据库没有启用MSDTC。。------------... 阅读全文
posted @ 2012-12-18 15:29 chear 阅读(510) 评论(0) 推荐(0)
摘要:var query = (from s in ctx.Students.Include("ClassRooms") join sd in ctx.StudentDescriptions on s.StudentID equals sd.StudentID into g from stuDesc in g.DefaultIfEmpty() select new { ... 阅读全文
posted @ 2012-12-15 13:09 chear 阅读(5324) 评论(1) 推荐(1)
摘要:code first 单表自链接 阅读全文
posted @ 2012-11-27 10:55 chear 阅读(168) 评论(0) 推荐(0)
摘要:Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.出现这样的错误就需要删除数据库在运行代码即可重新生成数据库。通常的原因是因为你的数据库中已经存在了部分数据,但是并没y响应的描述数据所造成,注意一下,在数据库中增加了一个名为 EdmMetadata 的表。你可以直接删除原来的数据库,重新运行 阅读全文
posted @ 2012-11-09 11:15 chear 阅读(464) 评论(0) 推荐(0)
摘要:将 FOREIGN KEY 约束 'FK_dbo.' 引入表 ' ' 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。WillCascadeOnDelete 取消级联删除。解决办法:modelBuilder.Entity<Company>().HasMany(t => t.Users).WithRequired(p => p.Company).WillCascadeOnDelete(false) ;详细代码介绍:http:/ 阅读全文
posted @ 2012-11-09 10:36 chear 阅读(11416) 评论(0) 推荐(0)