如何在实体框架中添加remove()和.add()?
当我在“实体框架”中使用时,我如何在我的repo/controler逻辑中编写和.add(对象数据)
这是我的代码示例,带有空的方法占位符:
class Projects
{
public static List<TProjekty> GetAll()
{
Stopwatch sw = new Stopwatch();
sw.Start();
EntitiesModel em = DB.GetDB();
List<TProjekty> projects = em.TProjekties.ToList();
sw.Stop();
Console.WriteLine("GetAllProjects: " + sw.ElapsedMilliseconds);
return projects;
}
public static List<TProjekty> GetAllActiveOnly()
{
Stopwatch sw = new Stopwatch();
sw.Start();
EntitiesModel em = DB.GetDB();
List<TProjekty> projects = em.TProjekties.Where(p => p.Aktualni == true).ToList();
sw.Stop();
Console.WriteLine("GetAllProjects: " + sw.ElapsedMilliseconds);
return projects;
}
public void Add(object data)
{
//var item = Entry(data);
}
public void Delete(object data)
{
//var item = Entry(data);
}
public static void Save(TProjekty project)
{
if (project == null)
{
return;
}
EntitiesModel em = DB.GetDB();
em.Add(project);
em.SaveChanges();
Checker.CheckAfterEverySave();
}
}
代码还不错,没有任何错误,但是我对add
和delete
的空白方法,因为我不知道我应该写它。我必须使用.NET Framework 4.8,因此我不能为.NET Core使用DB选项。
How do I can write a .Remove(object data)
and .Add(object data)
methods in my Repo/Controller logic when I am using in Entity Framework as the ORM? I generated Entity Model and everything works in my program, except for saving and removing entries from the database.
This is my code sample with the empty method placeholders:
class Projects
{
public static List<TProjekty> GetAll()
{
Stopwatch sw = new Stopwatch();
sw.Start();
EntitiesModel em = DB.GetDB();
List<TProjekty> projects = em.TProjekties.ToList();
sw.Stop();
Console.WriteLine("GetAllProjects: " + sw.ElapsedMilliseconds);
return projects;
}
public static List<TProjekty> GetAllActiveOnly()
{
Stopwatch sw = new Stopwatch();
sw.Start();
EntitiesModel em = DB.GetDB();
List<TProjekty> projects = em.TProjekties.Where(p => p.Aktualni == true).ToList();
sw.Stop();
Console.WriteLine("GetAllProjects: " + sw.ElapsedMilliseconds);
return projects;
}
public void Add(object data)
{
//var item = Entry(data);
}
public void Delete(object data)
{
//var item = Entry(data);
}
public static void Save(TProjekty project)
{
if (project == null)
{
return;
}
EntitiesModel em = DB.GetDB();
em.Add(project);
em.SaveChanges();
Checker.CheckAfterEverySave();
}
}
The code is alright without any errors, but I have empty methods for the Add
and the Delete
, because I have no idea how should I write it. I have to use .Net framework 4.8 so I cant use DB option for .Net Core.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该为此使用类型的界面,而不是尝试使用盒装对象引用,实际上您的代码在
save>保存
中已经在执行添加
>为你!如上所述,
保存
方法已经执行add
操作,因此实际上根本不会更新记录。保存
的此代码实际上应执行以下步骤顺序:架构和
dbContext
尚未提供实现,因此,如果您使用基于简单的整数id
键,我们在这里无法提供确定的解决方案,那么我们可以使用公共惯例在id
中检查零值:You should use a typed interface for this, instead of trying to use a boxed object reference, in fact your code in
Save
is already doing theAdd
for you!As mentioned above, the
Save
method was already performing theAdd
operation, so it won't actually be updating records at all. This code forSave
should actually perform the following sequence of steps:The schema and
DbContext
implementations have not been provided, so we can't offer a definitive solution here, if you are using a simple integer basedId
key, then we could use the common convention of checking for a zero value in theId
:您会像代码示例一样使用通用。
添加方法的签名应为:
You shoul use Generic like in your code example.
The signature for the Add method should be :