Include fields from related tables - ef-core-2.1

I'm using EF with DotNet Core 2.1 in my application. The application deals with data in multiple related and with FK interconnected tables.
I need audit logging data only changes to one table. However, my problem is, the table I need to audit log has quite some FK and for each of these I would like to log the FK itself and a field from the related table.
Let me try illustrate what I'm about - let's suppose this is my model:
public class Blog {
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
[InverseProperty ("Blog")]
public ICollection<Post> Posts { get; set; }
public Blog() {
Posts = new Collection<Post> ();
}
}
...
[AuditInclude]
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
[Required]
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
As said, I'd like audit logging only changes to one entity - let's say it is a Post - here is an audit class:
public class Audit_Post : IAudit {
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public string Blog { get; set; } // <- I need populating this from Blog.Name
[StringLength (64)]
public string AuditUsername { get; set; }
public DateTime AuditDt { get; set; }
public string AuditAction { get; set; }
public Audit_Manufacturer () { }
}
And this is how I set up audit logging in my startup.cs -> ConfigureService():
...
Audit.Core.Configuration.Setup ()
.UseEntityFramework (ef => ef
.AuditTypeExplicitMapper (m => m
.Map<Post, Audit_Post> ((d, al) => {
al.Blog = d.Blog?.Name; // !! This doesn't work
})
.AuditEntityAction<IAudit> ((evt, entry, auditEntity) => {
Object val;
var gotVal = evt.CustomFields.TryGetValue ("AuditUsername", out val);
string username = null;
if (gotVal && val is string)
username = val as string;
else
username = "<anonymous>";
auditEntity.AuditDt = DateTime.UtcNow;
auditEntity.AuditUsername = username;
auditEntity.AuditAction = entry.Action;
})
)
);
question: Is it possible at all to get and audit log data from dependant table (one-to-many) relation?
Beside the mentioned issue, I'm also bumped in an off-topic one, which is - if I forget updating the DB with the migration for initialising the Audit_Posts table and I'm doing operations on Posts table, the data get stored to the later even if audit logs fail to get written (UnitOfWork save exception). Is there a flag to AuditDbContext that would make it run in the same transaction as the original query?

As #thepirat000 pointed out, it is enough to guarantee all related items being present in the DbContext memory. This means:
INSERT Just before doing context.Posts.Add(item) do a query to all related items such as context.Blogs.Find(item.BlogId).
UPDATE When retrieving the Post, do it with .Include(d => d.Blog) + other related items.
DELETE When retrieving the Post, do it with .Include(d => d.Blog) + other related items.
An additional important thing that was causing me troubles is the layout of my Audit table. The issue was I reused the same property name in Audit table with a different type - in the original table the property Blog was a relationship property, whiles in the audit table it was a string. This caused errors in conversion from one to the other model.
[AuditInclude]
public class Post
{
...
[Required]
public int BlogId { get; set; }
public Blog Blog { get; set; }
...
}
Just rename it to something else like:
public class Audit_Post
{
...
public int BlogId { get; set; }
public string BlogName { get; set; }
...
}
...
// and in startup.cs use ...
...
.Map<Post, Audit_Post> ((d, al) => {
al.BlogName = d.Blog?.Name;
})
...
Regarding the 2nd issue - running audit inside transactions. I decided not to use it for now. I'll get covered the described case with tests.
Maybe a suggestion for future development of the package - it would be nice to have mentioned cases covered easily - I mean, transitive properties.

Your code should work fine if you Include the Blog property when retrieving the Post entity, for example:
using (var context = new BlogsContext())
{
var post = context.Posts
.Include(p => p.Blog) // Important, otherwise post.Blog will be NULL
.First();
post.Content += " adding this";
context.SaveChanges();
}
If you can't include the Blog entity because any reason, you could do the query on the mapping, but you will need to use a lower level overload on the Map method, like this:
.Map<Post, PostAudit>((ev, entry, postAudit) =>
{
var entryEf = entry.GetEntry();
var post = entryEf.Entity as Post;
var dbContext = entryEf.Context as BlogsContext;
// Get the blog related
var blog = dbContext.Blogs.FirstOrDefault(b => b.Id == post.BlogId);
postAudit.Blog = blog?.Name;
})
Regarding the other question, currently there is no built-in mechanism to rollback your database changes when the audit saving fails, but maybe you try with the AuditDbContext overrides

Related

MySQL / EF 6.0 Core One-to-One relationship returns null

I am using EF Core 6.0 with MySQL (Pomelo.EntityFrameworkCore.MySql v6.0)
I am setting up my database via code first. Here are my 2 models (simplified):
public class Store : BaseEntity
{
public string Name { get; set; }
public virtual User? Owner { get; set; }
public int? OwnerId { get; set; }
}
public class User : BaseEntity
{
public string? Name { get; set; }
public virtual Store? Store { get; set; }
}
For both, I have
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
as the primary key (from BaseEntity).
I also have Lazy Loading enabled here:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
}
and in the Program.cs:
builder.Services.AddDbContext<AppDbContext>(opt => opt
.UseLazyLoadingProxies()
.ConfigureWarnings(warning => warning.Ignore(CoreEventId.DetachedLazyLoadingWarning))
.EnableSensitiveDataLogging()
.UseMySql(
Globals.DB_CONNECTION_STRING, new MySqlServerVersion(Globals.MYSQL_SERVER_VERSION),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
));
Now, to the problem - I am trying to get a store from the database, using the following code:
Store? store = await dbContext.Stores
.Include(x => x.Owner)
.FirstOrDefaultAsync(x => x.Owner.Id == ownerId && x.Id == storeId);
I am getting the store details, but the Owner object and OwnerId is null. I can see the data in the database (e.g. I see OwnerId is set up for this specific store), but in the code, it is null.
I read on SO that Pomelo has some issues with setting up navigation properties, so I set it manually in OnModelCreating as follows:
modelBuilder.Entity<User>()
.HasOne(x => x.Store)
.WithOne(x => x.Owner)
;
But that didn't do the trick.
The same configuration works perfectly with MSSQL.
Any thoughts?
Thanks
Call HasForeignKey after WithOne and point to OwnerId.

How to create a dependency graph using composite keys in EF Core

Trying to store a composite key table which is keyed for both fields to the table it defines dependencies for.
Example case
Import files: 1..10
Dependencies 1: 2,3; 2: 4,5; 4:10
Intent is to use this key-only table for code to do code first strongly typed definitions while also being light weight, and it seemed like the most straight forward way to do it before running into problems.
Current code:
public class ImportFileDependency
{
[Key]
[ForeignKey("ImportFile")]
public int ImportFileId { get; set; }
[ForeignKey("Id")]
public ImportFile ImportFile {get; set;}
[Key]
[ForeignKey("ImportFile")]
public int ImportFileDependencyId { get; set; }
[ForeignKey("Id")]
public ICollection<ImportFile> ImportFileDependencies { get; set; }
}
public class ImportFile
{
[Key]
public int Id {get; set;}
public string Name { get; set; }
public string WorkbookTab { get; set; }
public string File { get; set; }
public ICollection<ImportFileDependency> Dependencies { get; set; }
}
...
modelBuilder
.Entity<ImportFileDependency>(e =>{
e.HasKey(ifd => new { ifd.ImportFileId, ifd.ImportFileDependencyId });
e.HasOne(ifd => ifd.ImportFile)
.WithMany(i => i.Dependencies);
});
modelBuilder
.Entity<ImportFile>()
.HasMany(i => i.Dependencies)
.WithOne()
.HasForeignKey(z => z.ImportFileId);
...
After multiple revisions of following the responses of the add-migration exception response, currently on:
There are multiple properties pointing to navigation 'ImportFile' in entity type 'ImportFileDependency'. To define composite foreign key using data annotations, use ForeignKeyAttribute on navigation.
which did not update from the most recent iteration.
I seem to have recursed into a deadend so looking for guidance
Given the time you've asked it, you probably found the answer yourself or gave up on it, but if someone else struggles with this error, this solved my issue: Entity Framework Code First - two Foreign Keys from same table
You have to define the relationship using fluent API.

Return a non-mapped Entity property based on a SUM of child records in the EntityFramework

Using an example, I have the following two Entities. The OrderEntity contains a collection of OrderLineEntites
public class OrderEntity
{
public string Reference { get; set; }
public string Description { get; set; }
public bool Confirmed { get; set; }
[NotMapped]
public int OrderLineCount { get; set; }
[InverseProperty("Order")]
public virtual ICollection<OrderLineEntity> OrderLineEntity__OrderEntity { get; set; }
}
public class OrderLineEntity
{
public string Name { get; set; }
public string Description { get; set; }
}
Using the following code I can load all the OrderLineEntities for all confirmed orders.
DbSet<OrderEntity> orderEntity.Where(x => x.Confirmed).Include(x => x.OrderLineEntity__OrderEntity)
What I need to do is set the non-mapped OrderLineCount property with the Count of the OrderLine records (to save actually loading them).
So for each loaded Order I have a fully populated Entity including the [NotMapped] property with an empty OrderLine collection.
Advise would be appreciated :)
Thanks
You can do this, but you have to change your approach. You have to manually map the objects yourself:
var query = from a in context.Orders.Where(x => x.Confirmed)
select new OrderEntity
{
Reference = a.Reference,
Description = a.Description,
Confirmed = a.Confirmed,
OrderLineCount = a.OrderLineEntity__OrderEntity.Count
};
return query.ToList();

EntityFramework Include and possibly join?

I have the following table structure as shown in the picture. (see: Table structure). Both tables ("Batches" and "Methods") reference to a "Project" table.
When I now create a new Project I would like to get all childs created as well.
Doing so I did the follwoing:
_dbContext.Projects.Where(x => x.Id == prjId)
.Include(x => x.Batches)
.Include(x => x.Batches.Select(y => y.Measurements))
.Include(x => x.Methods).AsNoTracking().FirstOrDefault();
Now the problem is the following:
New Batch and Method instances are created - thus they get a new ID(PK). The referenced Project_Id (FK) is set correct. But in my new Measurement instance only the Batch_Id(FK) is set correct and the Method_Id remains unchanged (has the old value) (see: result).
What I need is that the Measurements.Mehtod_Id is set from the Methods table. Is there any suitable solution for that?
My entities look like the following
public class Project
{
[Key]
public long Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public virtual List<Batch> Batches { get; set; }
public virtual List<Method> Methods { get; set; }
}
public class Batch : BaseObject
{
public Batch()
{
BatchFiles = new List<FileAttachment>();
Measurements = new List<Measurement>();
}
public long Id { get; protected set; }
public long Project_Id { get; set; }
public virtual Project Project { get; set; }
public virtual List<Measurement> Measurements { get; set; }
}
public class Method : BaseObject
{
public Method()
{
Parameters = new List<Parameter>();
}
public long Id { get; protected set; }
public long Project_Id { get; set; }
public virtual Project Project { get; set; }
public virtual List<Measurement> Measurements { get; set; }
}
public class Measurement
{
public int Id { get; protected set; }
[ForeignKey("Batch")]
public long? Batch_Id { get; set; }
[Required]
public virtual Batch Batch { get; set; }
[ForeignKey("Method")]
public long? Method_Id { get; set; }
public virtual Method Method { get; set; }
}
// creation code (just a copy with new IDs for all childs)
Project newProjectVersion = _dbContext.Projects.Where(x => x.Id == prjId)
.Include(x => x.Batches)
.Include(x => x.Batches.Select(y => y.Measurements))
.Include(x => x.Methods)
.AsNoTracking().FirstOrDefault();
_dbContext.Projects.Add(newProjectVersion);
_dbContext.SaveChanges();
Thanks for any help!
The first problem is that your Select statement doesn't connect Measurements to Methods because of the AsNoTracking() addition. Only Projects and Methods are connected because they are explicitly Included off of the Project entity. The Measurements have a Method_id but this is value is not accompanied by a Method in their Method property. You could check that in the debugger if you walk through the object graph (with lazy loading disabled though!). Because of this, when all entities will be Add-ed to the context, EF won't notice that measurements receive new methods.
You could get tempted to fix that by Include-ing Measurement.Method as well:
...
.Include(x => x.Batches.Select(y => y.Measurements.Select(m => m.Method)))
...
Now you'll see that Measurement.Method will be populated everywhere in the object graph.
However, there's a gotcha here. When using AsNoTracking, EF6 doesn't keep track of entities it materialized (duh). This means that for each Measurement it creates a new Method instance, even if an identical Method (by id) was materialized before for another Measurement. (And in this case it will always materialize duplicates, because you already include Project.Methods.)
That's why you can't do this in the quick way with AsNoTracking and Add using one context instance. You'll get an error that EF tries to attach duplicate entities.
You must build the object graph using one context, with tracking, so EF will not materialize duplicates. Then you must Add this object graph to a new context. Which will look like this:
Project project;
using(var db = new MyContext())
{
db.Configuration.ProxyCreationEnabled = false;
project = db.Projects.Where(x => x.Id == prjId)
.Include(x => x.Batches)
.Include(x => x.Batches.Select(y => y.Measurements))
.Include(x => x.Methods).FirstOrDefault();
}
using(var db = new MyContext())
{
db.Projects.Add(project);
db.SaveChages();
}
Three remarks:
Proxy creation is disabled, because you can't attach a proxy to another context without explicitly detaching it first.
No, I didn't forget to include Measurement.Method. All methods are loaded by including them in the Project and now (because of tracking, and assuming that measurement will only have methods of the project they belong to), EF connects them with the Measurements by relationship fixup.
EF-core is smarter here: when adding AsNoTracking it won't track materialized entities, but still, it won't create duplicates either. It seems to have some temporary tracking during the construction of an object graph.
thanks for your answer so far. This works quite fine right now. Unfortunately I noticed that the Measurements entity has another required relationship to a table named 'MeasurementTypes':
[Required]
public virtual MeasurementType MeasurementType { get; set; }
[ForeignKey("MeasurementType")]
public long MeasurementType_Id { get; set; }
In contrast to Batches and Methods these entries must not be copied and the entries already exist in the MeasrementTypes table.
What would be a good way to put the required reference to the Measurements?

How to use properly the ChangeTracker.Entries<Entity> in Entity Framework Code First

this is my simple DbContext inheriting class:
public class School : DbContext
{
public DbSet<Activity> Activity { get; set; }
public DbSet<Student> Student { get; set; }
public override int SaveChanges()
{
string s = string.Empty;
foreach (var entry in ChangeTracker.Entries<Activity>().Where(a => a.State != EntityState.Unchanged))
s = entry.State.ToString();
foreach (var entry in ChangeTracker.Entries<Student>().Where(a => a.State != EntityState.Unchanged))
s = entry.State.ToString();
return base.SaveChanges();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Configuration.ValidateOnSaveEnabled = false;
Configuration.LazyLoadingEnabled = true;
base.OnModelCreating(modelBuilder);
}
}
these are my entites:
public class Student
{
public int id { get; set; }
public string Name { get; set; }
public string Roll { get; set; }
//naviagtional property
public virtual IList<Activity> Activities { get; set; }
}
public class Activity
{
public int id { get; set; }
public double Maths { get; set; }
public double Science { get; set; }
public double History { get; set; }
//navigational property
public virtual Student Student { get; set; }
}
somewhere in my code i do this:
int studentId = Convert.ToInt32(Request.Form["Student.id"]);
Activity activity = dbContext.Activity.Where(e => e.Student.id == studentId).Single();
activity.Student.Name = Request.Form["Student.Name"];
activity.Student.Roll = Request.Form["Student.Roll"];
activity.Maths = Convert.ToDouble(Request.Form["Maths"]);
activity.Science = Convert.ToDouble(Request.Form["Science"]);
dbContext.SaveChanges();
Everything's normal and fine and works as it should. My question is, by updating activity.Student.Name, how can I detect change in Activity entity and not in Student entity? Is there any support in Entity Framework to detect changes in the parent table (and not in the slave table, where actual change goes though).??
Please help, it will save me a lot of time..
Even though this is an older question I thought I would give an answer anyway. NO you cannot.
The reasoning behind this is that you the programmer, as far as the code example goes is aware what is happening and could act on that (before doing the SaveChanges) to make sure whatever you want to happen is going to happen.
The Student you are changing might also be part of other entities, so would you also want those entities to be notified. An automatic behavior as you suggest would result in very complex notifications begin sent through the model which is (in most cases undesirable).
As #Ladislav Mrnka also indicated youy did not change the activity, but a Student involved in the activity. If the student relation is more than a simple lookup perhaps the model should be changed. Form the sample code given it is hard to see "why" you would need to detect changes made "through" other entities