如何在不拉动所有对象的情况下编辑或添加到特定字段 [英] How can I edit or add to a particular field without pull the all object

查看:66
本文介绍了如何在不拉动所有对象的情况下编辑或添加到特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 如何做到这一点( a.myFavorits.Add())而不将所有对象都拉到var a ,因为 a 有很多数据,我不想提取所有 a 对象,但是我找不到解决方法。

  1. How I can do just this ( a.myFavorits.Add()) without pulling the all object to var a , because a has a lot of data, and I don't want to pull all a object, but I can't find a way do do it.

我想做lambada和linq而不会返回但是linq总是返回某些东西

I want to do the lambada and the linq without return something but linq is always return something

public static void addFavorits(long f,long idUser)
{
    using (var db = dataBase())
    {
       // here i pull object user from users table
        var a = db.users.Where(c => c.id == idUser).SingleOrDefault();

       // here i adding  to the object field  myFavorits new value
        //myFavorits is also a table of entitys that connected to user object

        a.myFavorits.Add(new BE.FavoritsUsersLong { myLong = f });

         db.SaveChanges(); 
    }
}


我想做这样的事情,但我不知道如何设置字段 users_TableId ,这是连接2个表的关键

I thought to do something like this but i dont know how to set the field users_TableId that is the key that connect the 2 tables

public static void addFavorits(long favoritId,long idUser)
{
    using (var db = dataBase())
    {
        db.favoritsUsersLong.Add(new BE.FavoritsUsersLong {myLong = favoritId} 
               /*,users_TableId =idUser*/);

        db.SaveChanges(); 
    }
}

推荐答案

这是一个具体的示例,可以满足您的需求。在此示例中,仅修改并保存了公司名称。或将某项添加到其集合中。

Here's a concrete example that does what you want. In this example, only the Name of a Company is modified and saved. Or an item is added to one of its collections.

var cmp = new Company{ CmpId = 1, Name = "Cmp1" }; // CmpId is the primary key
db.Companies.Attach(cmp);
db.Entry(cmp).Property(c => c.Name).IsModified = true;

// Or add an entity to a collection:
cmp.Users = new[] {new User { Name = "a1", PassWord = "a1" } };

try
{
    db.Configuration.ValidateOnSaveEnabled = false;
    db.SaveChanges();
}
finally
{
    db.Configuration.ValidateOnSaveEnabled = true;
}

SQL的结果:

DECLARE @0 VarChar(30) = 'Cmp1'
DECLARE @1 Int = 1
UPDATE [dbo].[Company]
SET [Name] = @0
WHERE ([CmpId] = @1)

这里要注意几件事:


  • 显然,您需要知道要修改的实体的ID

  • 您创建的对象称为 stub实体,这是一个不完整的实体。当您尝试保存这样的实体时,EF很可能会抱怨必需属性中的空值。因此,几乎可以肯定的是,您必须禁用验证(暂时或更好的方法是立即释放上下文)。

  • 如果要将项目添加到集合中,则应保留验证启用,因为您想确定新实体是否有效。因此,您不应混合使用这两种方法来使用存根实体。

  • 如果您经常需要实体的大致相同的一小部分,则可以考虑表拆分

  • Obviously you need to know the Id of the entity you want to modify.
  • The object you create is called a stub entity, which is an incomplete entity. When you try to save such an entity, EF is very likely to complain about null values in required properties. That's why almost certain you'd have to disable validation (temporarily, or, better, dispose the context immediately).
  • If you want to add an item to a collection, you should leave validation enabled, because you'd want to know for sure that the new entity is valid. So you shouldn't mix these two ways to use a stub entity.
  • If you often need roughly the same small part of your entity you may consider table splitting.

这篇关于如何在不拉动所有对象的情况下编辑或添加到特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆