Logging every data change with Entity Framework
How about handling Context.SavingChanges?
How about handling Context.SavingChanges?
Creating filtered index excluding NULL values is the default EF Core behavior for unique indexes containing nullable columns. You can use HasFilter fluent API to change the filter condition or turn it off by passing null as sql argument: entityBuilder.HasIndex(e => new { e.LevelId, e.Name, e.DeletedAt }) .IsUnique() .HasFilter(null);
Symfony 4+, 2019+ Approach In symfony 4 (probably 3.3 also, but only real-tested in 4) you can inject the Security service via auto-wiring in the controller like this: <?php use Symfony\Component\Security\Core\Security; class SomeClass { /** * @var Security */ private $security; public function __construct(Security $security) { $this->security = $security; } public function privatePage() : Response … Read more
It will be negative until you save your changes. Just call Save on the context. _dbContext.Locations.Add(location); _dbContext.Save(); After the save, you will have the ID which is in the database. You can use transactions, which you can roll back in case there’s a problem after you get the ID. The other way would be not … Read more
You must define the ParentId in the category class as nullable to use it as the foreign key property for an optional relationship: public int? ParentId { get; set; } An int property cannot take the value null and therefore cannot represent a NULL as value in a database column.
You can customize the generated code by sub-classing the CSharpMigrationCodeGenerator class: class MyCodeGenerator : CSharpMigrationCodeGenerator { protected override void Generate( DropIndexOperation dropIndexOperation, IndentedTextWriter writer) { dropIndexOperation.Table = StripDbo(dropIndexOperation.Table); base.Generate(dropIndexOperation, writer); } // TODO: Override other Generate overloads that involve table names private string StripDbo(string table) { if (table.StartsWith(“dbo.”)) { return table.Substring(4); } return table; } … Read more
may be too late to answer but it may help others, EF 4.0 uses the ObjectContext class where as the version 4.1 uses the DbContext class in which the methods like Set<T> and Entry are defined. With version 4.0 you can do something like DatabaseContext _context = new DatabaseContext(); _context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); with version 4.1 its … Read more
It works indeed as described but the article on MSDN misses to emphasize that it only works if the children are loaded into the context as well, not only the parent entity. So, instead of using Find (which only loads the parent) you must use eager loading with Include (or any other way to load … Read more
This occurs in CodeFirst because of the virtual keyword. In effect, you are creating a relationship where creating one item requires the creation of the other. however, the virtual keyword allows lazy instantiation, which means that creating an object of one type doesn’t automatically create the other type, allowing the Id on the foreign item … Read more
The EF Core code is exactly the same. DbContext.Database.BeginTransaction(IsolationLevel.Snapshot); The only difference is that in EF Core the method with isolation level (as many others) is an extension method, defined in RelationalDatabaseFacadeExtensions class, and importantly, located in Microsoft.EntityFrameworkCore.Relational assembly. So if you have using Microsoft.EntityFrameworkCore; and don’t see it, add reference to the Microsoft.EntityFrameworkCore.Relational.dll assembly … Read more