Doubt - Savechanges does not work, only returns 0

Asked

Viewed 170 times

4

Follows code:

using (var db = new Entities())
{
    var result = db.
        Tabela1
        .Where(x => x.Id == 1)
        .Select(x => new SuaClasse
        {
            Coluna1 = x.Coluna1,
        })
        .FirstOrDefault();

    if (result.Coluna1 != string.Empty)
    {
        result.Coluna2 = "Novo valor";
    }               

    var num = db.SaveChanges(); // aqui retorna 0
}

Class:

public class SuaClasse 
{
    public string Coluna1 { get; set; }
    public string Coluna2 { get; set; }
}

I just want to select a column and then update column value2, nothing happens, only returns 0. What I did wrong ?

2 answers

7

You do not need to select an anonymous object or a different class to then perform an update.

Just run select, change the field, and then update.

using (var db = new Entities())
{
    var result = db.
        Tabela1
        .Where(x => x.Id == 1).FirstOrDefault();

    if (result.Coluna1 != string.Empty)
    {
        result.Coluna2 = "Novo valor";
    }               

    var num = db.SaveChanges(); // aqui retorna 0
}
  • @Matheusmiranda this varbinary is image ? file ? by what I’ve been reading, to get around this problem you should put this property in another entity, even if it is 1:1. Then it is more a matter of your need... whether it is easier to make another entity or use the query. Vlw

1


The Entity Framework do not change anonymous objects, and yes only changes the one he knows and is contained in his context. In this code for example, it happens that the creation of a type that the Entity you don’t know.

If you need to change fields individually, you can write the update and send the Entity Framework execute.

Example:

using (var context = new BloggingContext()) 
{ 
    context.Database.ExecuteSqlCommand( 
        "UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1"); 
}

can also bring information, example:

using (var context = new BloggingContext()) 
{ 
    var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); 
}

and the way that the Entity Framework makes is materialize the whole object (as already explained, contained in its context) and then you can make changes to the field and have it saved, in your code something like this:

using (var db = new Entities())
{
    var result = db.Tabela1.Where(x => x.Id == 1).FirstOrDefault();
    if (result != null)
    {
        result.Coluna2 = "Novo valor";
        var num = db.SaveChanges();
    }
}

Reference:

  • 1

    One of the advantages of using the Entity Framework is not avoiding having to use SQL?

  • 1

    @ramaral you asked well, avoid, but, does not mean that can not use, and in systems development we do not have a full rule for everything, this way for example it is perhaps better to materialize objects and only change a field... It will depend on the occasion, but it is not a rule that can not be used as much as the framework promotes this.

  • Now the answer is almost as I expected it to be: use Savechanges()` and alternatively, when justified, use raw SQL.

  • The hard part is that people end up not understanding @ramaral, just wanted to demonstrate that there are paths that can be followed, but not as a general rule. Maybe he has doubts about how it is this or that, I think my context is better explained, but, many do not see it that way and we have to respect, thank you for your comment.

  • Virgilio, don’t misunderstand my comments. Your answer is/was good, because it started with the explanation of why SaveChanges() does not work. I just think that instead of having presented a solution with raw SQL, I should have presented one with SaveChanges(). The alternative with SQL is valid and useful in several situations but there has to be a justification to use it, the question did not indicate any.

  • @ramaral did not misunderstand the comments, the question did not indicate, and did not say a way, was open, I missed putting the third option, which would be implicit in the development with Entity Framework, but, that here should be observed.

  • That’s it, it seems then that we agree:)

  • Virgil, why is your first example of code or second example not recommended to use (some say) ? More works! Because of the ORM ?

  • 1

    @Matheusmiranda is recommended to use at times that need to be used, there is a rule not to use, but, there is where it is necessary to use, an example of using this if you have to change a lot of information where you need to set a default value to 1000 records, the first two answer options perform better than the 3rd way. In your specific case with the search should return an item, it is perhaps in this case the best, because the only changes an item of your table, so saying "Not recommended" is wrong, it can be used or does not depend on the cases ...

  • 1

    @Matheusmiranda is not only working, it is using the right way and time ... understood! The 3 options are functional and correct, each in its moment. Your question is rather why the examples.

  • I understand, I have a field with very large file with varbinary type, is a video. So I used Select() to avoid the big video. In this case it is recommended to use in the first example of your code ? Because with Entity framework without Select() brings all fields, hence the memory does not thank.

  • 1

    @Matheusmiranda can use the first or the third, as you will alter one video at a time I believe these two are satisfactory. To not have additional work, do with the 3 option, because if do with the first one you must create a Sqlparameter and tell the type to record. Now if you understand that recovery is very slow and just want to change, you can use the first one. Take the test to see if there is much difference in performance.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.