As the context (DbContext
) works when I call an entity by First()
, FirstOrDefault()
, Find(value)
, that is, I bring a record for change:
Model:
[Table("Credit")]
public class Credit
{
public Credit()
{
}
public Credit(string description)
{
Description = description;
}
public Credit(int id, string description)
{
Id = id;
Description = description;
}
[Key()]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required()]
[MaxLength(50)]
public string Description { get; set; }
public bool Status { get; set; }
public int Lines { get; set; }
}
Context ():
public class Database : DbContext
{
public Database()
:base(@"Server=.\SqlExpress;Database=dbtest;User Id=sa;Password=senha;")
{
Database.Initialize(false);
}
public DbSet<Credit> Credit { get; set; }
}
Running operations:
class Program
{
static void Main(string[] args)
{
using (Database db = new Database())
{
db.Database.Log = ca => WriteLog(ca);
int value = 1;
Credit cr = db.Credit.Find(value);
cr.Lines = cr.Lines + 1;
db.SaveChanges();
}
System.Console.WriteLine("Pression <Enter> ...");
}
public static void WriteLog(string c)
{
System.Console.WriteLine(c);
}
}
In the above code is used the basic audit which shows which SQL
are being generated, in the case example are two a Select
and a Update
, being an update only of what has been modified, as demonstrated in debug
just below:
Then conclude that the update
only the property that has been changed.
1 - Context has some form to generate the same syntax?
Yes it generates the same syntax, updating only the properties that have been changed.
2 - Is there any way to see the query that is being sent to the bank in this case?
Yes exite, db.Database.Log
will bring the SQL
generated by context.
using (Database db = new Database())
{
db.Database.Log = ca => WriteLog(ca);
Can also be used in the Debug Trace
:
db.Database.Log = ca => System.Diagnostics.Debug.WriteLine(ca);
3 - How is done by Context to let it know that only the column Name has changed?
Credit cr = db.Credit.Find(value);
cr.Lines = cr.Lines + 1;
db.Entry(cr).Property(x => x.Lines).IsModified
This shows db.Entry(cr).Property(x => x.Lines).IsModified
if the property has undergone any alteration, the Context is based on the truth answer (true
) and the code is responsible for changing only what is modified as true
.
There is nothing implemented to check which fields are modified at once, but it can be done easily by an extension method to tell which fields have changed and which have not suffered:
public static class MethodsUpdate
{
public static Dictionary<string, bool> GetUpdatePropertyNames<T>(this DbEntityEntry<T> entry)
where T: class, new()
{
Dictionary<string, bool> entryUpdate =
new Dictionary<string, bool>();
foreach (string name in entry.CurrentValues.PropertyNames)
{
entryUpdate.Add(name, entry.Property(name).IsModified);
}
return entryUpdate;
}
}
Dictionary<string, bool> propertyChanges = db.Entry(cr).GetUpdatePropertyNames();
In this specific case the change in the bank has not yet been applied, only in its context.
References:
Question 2 complements Question 1, certain?
– Marllon Nasser
I practically just need to know how to see this query, some way to capture it. There’s not a lot of relation between 1 and 2. The first I want to know how it’s generated and the second I want to know if there’s any way I can capture it, copy it. understanding this process will help me solve this other
– Marco Souza
You are using SQL Server?
– Randrade
@Randrade, exactly, I don’t know if there’s any difference between banks.
– Marco Souza
has tools that SQL has exactly to help with that. I’ll be working out a more detailed answer here.
– Randrade
@Randrade, but is this through the bank? because the 2 question may be, but 1 and 3 would like to know the (properties , methods ) used by Context,
– Marco Souza
That question is not duplicated of this?
– Jéf Bueno
@jbueno, no... has the same goal, but and another thing , I spoke here
– Marco Souza
What a mess...
– Jéf Bueno
@jbueno, why ? is not clear the question? if I edited the other with that the more would become very large.
– Marco Souza
@jbueno, what I thought to solve the other case was the following, if I knew how it is done in the context for it to only change a certain field then have the possibility of I tell which fields I want to change with that I could select only from the fields I want and say which ones I want to change.
– Marco Souza