<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmad Ibrahim</title>
    <description>The latest articles on DEV Community by Ahmad Ibrahim (@a510).</description>
    <link>https://dev.to/a510</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F556802%2F6d49f5c2-211f-430f-82d7-49f5e371941e.png</url>
      <title>DEV Community: Ahmad Ibrahim</title>
      <link>https://dev.to/a510</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/a510"/>
    <language>en</language>
    <item>
      <title>.NET Term: Metapackage</title>
      <dc:creator>Ahmad Ibrahim</dc:creator>
      <pubDate>Sat, 07 Aug 2021 11:15:26 +0000</pubDate>
      <link>https://dev.to/a510/net-term-metapackage-4p7a</link>
      <guid>https://dev.to/a510/net-term-metapackage-4p7a</guid>
      <description>&lt;p&gt;A &lt;strong&gt;metapackage&lt;/strong&gt; is a NuGet package that references other NuGet packages and typically does not include any assemblies itself.&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://stackoverflow.com/a/45424820/2300466"&gt;From Stackoverflow&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>dotnet</category>
    </item>
    <item>
      <title>Programming Term: "glob"</title>
      <dc:creator>Ahmad Ibrahim</dc:creator>
      <pubDate>Sat, 07 Aug 2021 10:23:52 +0000</pubDate>
      <link>https://dev.to/a510/programming-term-glob-5234</link>
      <guid>https://dev.to/a510/programming-term-glob-5234</guid>
      <description>&lt;p&gt;&lt;strong&gt;glob patterns&lt;/strong&gt; specify sets of filenames with wildcard characters. For example, the Unix Bash shell command &lt;code&gt;mv *.txt textfiles/&lt;/code&gt; moves (&lt;code&gt;mv&lt;/code&gt;) all files with names ending in &lt;code&gt;.txt&lt;/code&gt; from the current directory to the directory &lt;code&gt;textfiles&lt;/code&gt;. Here, &lt;code&gt;*&lt;/code&gt; is a wildcard standing for "any string of characters" and &lt;code&gt;*.txt&lt;/code&gt; is a glob pattern.&lt;/p&gt;

&lt;p&gt;(&lt;a href="https://en.wikipedia.org/w/index.php?title=Glob_(programming)&amp;amp;oldid=1034211884"&gt;From Wikipedia&lt;/a&gt;)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Strategy Pattern Basic Example</title>
      <dc:creator>Ahmad Ibrahim</dc:creator>
      <pubDate>Fri, 30 Jul 2021 12:24:33 +0000</pubDate>
      <link>https://dev.to/a510/strategy-pattern-basic-example-3ao7</link>
      <guid>https://dev.to/a510/strategy-pattern-basic-example-3ao7</guid>
      <description>&lt;p&gt;The strategy pattern is used when a job can be done using different algorithms (strategies), and we need the ability to change the execution strategy dynamically and without changing the API that we're calling. Consider sorting an array of elements, where we have multiple sort algorithms (bubble sort, quick sort, etc..).&lt;/p&gt;

&lt;p&gt;Let's take an example of a traveler who can move from city to city using different means of transport (car, plane, bus, etc..). We might make a &lt;code&gt;Traveler&lt;/code&gt; class like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Traveler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TravelByCar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// implementation&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TravelByPlane&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// implementation&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TravelByBus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// implementation&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are some problems with the above code, firstly it violates the Open/Closed principle, as you have to modify the &lt;code&gt;Traveler&lt;/code&gt; class whenever you need to add another means of transport, secondly, we introduce a different API for each transport which forces the caller to specify the API whenever he uses it (maybe he's not concerned with this transport details).&lt;br&gt;
The strategy pattern solves this problems by separating the travel strategy from the traveler as follow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Traveler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ITransport&lt;/span&gt; &lt;span class="n"&gt;_transport&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Traveler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ITransporter&lt;/span&gt; &lt;span class="n"&gt;defaultTransport&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_transport&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;defaultTransport&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Travel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ChangeTransport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ITransport&lt;/span&gt; &lt;span class="n"&gt;newTransport&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_transport&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newTransport&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the API caller will provide a default strategy only at constructing the object and he will simply call one API afterwards.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;traveler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Traveler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BusTransport&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// pass the default strategy&lt;/span&gt;

&lt;span class="n"&gt;traveler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Travel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// use just a single API to travel&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The API user can also change the strategy without having to modify the existing class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;transport&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CarTrasnport&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;traveler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ChangeTransport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;He can also introduce new transports.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TrainTransport&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ITransport&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// transport implementation&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;...&lt;/span&gt; 

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;transport&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TrainTransport&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;traveler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ChangeTransport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dotnet</category>
      <category>codequality</category>
      <category>csharp</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>EF: When SaveChanges does not save changes</title>
      <dc:creator>Ahmad Ibrahim</dc:creator>
      <pubDate>Fri, 23 Jul 2021 13:27:49 +0000</pubDate>
      <link>https://dev.to/a510/ef-when-savechanges-does-not-save-changes-395</link>
      <guid>https://dev.to/a510/ef-when-savechanges-does-not-save-changes-395</guid>
      <description>&lt;p&gt;I was debugging an issue with &lt;code&gt;DbContext.SaveChanges&lt;/code&gt; which was neither saving changes to the database nor throwing any exceptions, and after solving the issue I found it interesting to list some of the reasons that might cause this issue and how to detect them.&lt;br&gt;
The possible reasons fall into four main categories.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wrong destination&lt;/li&gt;
&lt;li&gt;SaveChanges isn't called properly&lt;/li&gt;
&lt;li&gt;Changes are not tracked&lt;/li&gt;
&lt;li&gt;Database does not preserve changes&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Wrong destination
&lt;/h2&gt;

&lt;p&gt;You might be expecting your changes to be saved somewhere but your &lt;code&gt;DbContext&lt;/code&gt; is saving it somewhere else. For example the connection string is not the one you expect. It's a common mistake that some developers change the connection string in the configuration file of the data access project instead of the startup project.&lt;/p&gt;

&lt;p&gt;To check which connection string your DbContext is using you can do this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
// check the value of the connection string
string connection = dbContext.Database.GetConnectionString();
dbContext.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Another reason could be that you are checking the wrong database table, maybe your EF entity is mapped to another table (e.g. via some fluent API configuration), you can check that as follows.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
var mapping = dbContext.Model.FindEntityType(typeof(YourEntity));
string schema = mapping.GetSchema();
string table = mapping.GetTableName();

dbContext.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Make sure that &lt;code&gt;schema&lt;/code&gt; and &lt;code&gt;table&lt;/code&gt; are as you expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  SaveChanges isn't called properly
&lt;/h2&gt;

&lt;p&gt;You might be calling &lt;code&gt;SaveChanges&lt;/code&gt; on a different instance of your &lt;code&gt;DbContext&lt;/code&gt;, and that might not be easy to spot in some complex codebases where changes to the &lt;code&gt;DbContext&lt;/code&gt; are far from the call to &lt;code&gt;SaveChanges&lt;/code&gt;.&lt;br&gt;
EF assigns a unique ID for each &lt;code&gt;DbContext&lt;/code&gt; instance which could be used to check that.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
dbContext.Employees.Add(newEmp);
string id1 = dbContext.ContextId;

.....

// elsewhere in the codebase
string id2 = dbContext.ContextId;
dbContext.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You have to confirm that &lt;code&gt;id1&lt;/code&gt; equals &lt;code&gt;id2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another silly mistake might be calling &lt;code&gt;SaveChangeAsync&lt;/code&gt; without awaiting it (i.e. using await keyword) which absorbs exceptions and obviously to fix that you just need to await the call &lt;code&gt;await dbContext.SaveChangesAsync();&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changes are not tracked
&lt;/h2&gt;

&lt;p&gt;The EF Change Tracker is the component responsible for detecting the changes that should be updated in the database. There are many reasons why change tracker doesn't see/consider your changes. To check if this is the case you might do something like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
bool hasChanges = dbContext.ChangeTracker.HasChanges(); // should be true
int updates = dbContext.SaveChanges();                  // should be &amp;gt; 0


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When &lt;code&gt;hasChanges&lt;/code&gt; is false or &lt;code&gt;updates&lt;/code&gt; is 0 then the Change Tracker couldn't detect your changes, maybe because you're changing the value of a &lt;code&gt;NotMapped&lt;/code&gt; property like &lt;code&gt;FullName&lt;/code&gt; in the entity below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public string FullName { get; set; }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or maybe you are changing a disconnected/detached entity which is a common scenario in web applications when you receive an object from the client-side, one way to fix this is by changing the entry state.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
db.Entry(myEntity).State = EntityState.Modified; // use an appropriate state
dbContext.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Database not preserving changes
&lt;/h2&gt;

&lt;p&gt;One of the sneakiest issues is when you begin a database transaction but you don't commit it, it's sneaky because in this case you will see that the Change Tracker detected your changes (i.e. &lt;code&gt;hasChanges&lt;/code&gt; equals true, &lt;code&gt;updates&lt;/code&gt; will be greater than 0) and the SQL Server Profiler will show that the insert statement executed successfully on your database but still you won't find any changes, check this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
using (var context = new MyContext())
{
    var transaction = context.Database.BeginTransaction(IsolationLevel.Serializable);
    context.Employees.Add(newEmployee);
    int updates = context.SaveChanges();   // updates == 1 even when the transaction is not committed
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In my case that was the reason why &lt;code&gt;SaveChange&lt;/code&gt; wasn't working and the &lt;code&gt;BeginTransaction&lt;/code&gt; call was not as clear as in the above snippet, it was buried into a forsaken area in the codebase, where someone was trying something and forgot to remove it.&lt;br&gt;
To detect whether your &lt;code&gt;DbContext&lt;/code&gt; has a current transaction you can check if &lt;code&gt;context.Database.CurrentTransaction&lt;/code&gt; is not null.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
// use this to check if a database transaction has begun
var hasTransaction = context.Database.CurrentTransaction != null;
context.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When &lt;code&gt;hasTransaction&lt;/code&gt; is true you should commit your transaction by calling &lt;code&gt;transaction.Commit&lt;/code&gt; after calling &lt;code&gt;SaveChanges&lt;/code&gt;, otherwise your changes will not be saved.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
using (var context = new MyContext())
{
    var transaction = context.Database.BeginTransaction(IsolationLevel.Serializable);
    context.Employees.Add(newEmployee);
    int updates = context.SaveChanges();
    transaction.Commit(); // solves the problem
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;Another issue could be that there's something that reverts your changes like a database trigger. You can test that by disabling all triggers on your table &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 sql
Disable Trigger All on YourTableName


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or even on the whole database &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 sql
Disable Trigger All on Database.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can also check this in the SQL Server Profiler but you have to make sure that the &lt;code&gt;SP:StmtStarting&lt;/code&gt; and &lt;code&gt;SP:StmtCompleted&lt;/code&gt; events are selected in the "Trace Properties" dialog.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0n286zaix1e23ris9j06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0n286zaix1e23ris9j06.png" alt="Trace Dialog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Expand the "Stored Procedures" node (Yes, there's no triggers node), then check &lt;code&gt;SP:StmtStarting&lt;/code&gt; and &lt;code&gt;SP:StmtCompleted&lt;/code&gt; events.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4vqdab95q0r95n9s7qv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4vqdab95q0r95n9s7qv.png" alt="Trace Events"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there are any triggers that update your table, then you will see their update statements in the SQL profiler.&lt;/p&gt;

&lt;p&gt;Another prevalent issue (on Stackoverflow) in this category is when you are using LocalDb where the .mdf file could be overwritten every time you build your project, to fix that you need to change the CopyToOutput action to overwrite the file only if newer.&lt;/p&gt;




&lt;p&gt;The database might not preserve your changes if your changes affect a precision level that is not supported by the data type of the database field.&lt;/p&gt;

&lt;p&gt;A common example for this is the &lt;code&gt;datetime&lt;/code&gt; field in MS SQL Server which has less precision than the &lt;code&gt;DateTime&lt;/code&gt; object in .NET, check the code below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 csharp
myEntity.Time = DateTime.Parse("2020–01–01 13:45:59.2501234"); // the last 4 digits will never be saved
dbContext.SaveChanges();


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The update statement that EF generates for the above snippet will be like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 sql
exec sp_executesql N'SET NOCOUNT ON;
UPDATE [MyTable] SET [Time] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
',N'@p1 int,@p0 datetime',@p1=1,@p0='2020–01–01 13:45:59.250'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see the SQL statement doesn't include the fraction part of the &lt;code&gt;DateTime&lt;/code&gt; object. Note that this issue is one of the issues that can be spotted via the SQL Server profiler.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
