本文介绍了使用C#Truncate表的.NET Framework 4.5的实体框架5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图在用数据填充表之前截断表。我已经拥有了所有表格,可以用信息填充它。这就是我使用.edmx设置实体的方法图我选择[从数据库更新模型...]然后选择我需要的表格,它为我设置了每一个。 然后在代码中我使用它: MyEntities myE = new MyEntities(); SomeEntityTable table = new SomeEntityTable(); table.someFieldName1 = Value1; table.someFiledName2 = Value2; ... myE.Add(table); myE.SaveChanges(); 在我使用此表之前,有人可以帮我弄清楚如何添加一行在运行此代码之前截断表中的所有数据。我知道我需要获取一个上下文然后运行Truncate Table但不确定如何。 谢谢。解决方案 好的,这就是我做的。 MyEntities myE = new MyEntities(); // 这将清除您的表格,您的索引将从1开始。 var objCtx =((System.Data.Entity.Infrastructure.IObjectContextAdapter)myE).ObjectContext; objCtx.ExecuteStoreCommand( 截断表SomeEntityTable); SomeEntityTable table = new SomeEntityTable(); table.someFieldName1 = Value1; table.someFiledName2 = Value2; ... myE.Add(table); myE.SaveChanges(); 你可以使用下面提到的代码(这是一个样本) var objCtx =((System.Data.Entity.Infrastructure.IObjectContextAdapter)dataDb).ObjectContext; objCtx.ExecuteStoreCommand( TRUNCATE TABLE [Table]); 了解更多信息:实体框架。删除表格中的所有行 或 Truncate 我希望这会对你有所帮助。 检查这个示例应用程序: 初学者实施框架 [ ^ ] I am trying to Truncate table before populating it with data. I already have all the tables and can populate it with information. This is how I set up Entity using .edmx Diagram I choose [Update Model From Database ...] then select the table I need and it sets every up for me.Then in the code I use this:MyEntities myE = new MyEntities();SomeEntityTable table = new SomeEntityTable();table.someFieldName1 = "Value1";table.someFiledName2 = "Value2";...myE.Add(table);myE.SaveChanges();Could somebody help me figure out how to add a line before I use this table to truncate all the data in a table prior to running this code. I know I need to get a context then run Truncate Table but not sure how.Thanks. 解决方案 Okay here is what I did.MyEntities myE = new MyEntities();// This will clear your table and your index will start at 1.var objCtx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)myE).ObjectContext;objCtx.ExecuteStoreCommand("Truncate table SomeEntityTable");SomeEntityTable table = new SomeEntityTable();table.someFieldName1 = "Value1";table.someFiledName2 = "Value2";...myE.Add(table);myE.SaveChanges();You can use below mentioned code for that (this is a sample)var objCtx = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dataDb).ObjectContext;objCtx.ExecuteStoreCommand("TRUNCATE TABLE [Table]");for more info : Entity Framework. Delete all rows in tableORTruncateI hope this will help to you.Check this sample application:Entity Framework for Beginners[^] 这篇关于使用C#Truncate表的.NET Framework 4.5的实体框架5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 19:33