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?
Related
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.
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.
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
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
I noticed something interesting when I was performing a delete using EF code first. I use the following domain model:
public class User
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Playlist> Playlists { get; set; }
}
public class Playlist
{
public virtual long Id { get; set; }
public virtual string Title { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Track> Tracks { get; set; }
}
public class Track
{
public virtual long Id { get; set; }
public virtual string Title { get; set; }
public virtual Playlist Playlist { get; set; }
}
The model is configured using:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasMany(x => x.Playlists).WithRequired(x => x.User).Map(x => x.MapKey("UserId"));
modelBuilder.Entity<Playlist>().HasMany(x => x.Tracks).WithRequired(x => x.Playlist).Map(x => x.MapKey("PlaylistId"));
}
I use a generic repository:
public virtual void Delete(T entity)
{
Database.Set<T>().Remove(entity);
}
I also have a dto that looks like:
public class PlaylistDTO
{
public PlaylistDTO(Playlist playlist)
{
Id = playlist.Id;
Title = playlist.Title;
User = playlist.User.Name;
}
}
In one of my services I am trying to do the following:
public PlaylistDTO Delete(long id)
{
Playlist playlist = playlistRepository.GetById(id);
playlistRepository.Delete(playlist);
unitOfWork.Commit();
return PlaylistDTO(playlist);
}
This code fails. When I stepped through the debugger I noticed something interesting. The moment I call playlistRepository.Delete the navigational properties (User and Tracks) get set to null and empty respectively. Playlist however stays in memory. So when I pass in the playlist to the DTO the code will fail when it is trying to access playlist.User.Name. I wanted to pass this data to the client to display a verification.
Is this behavior correct? Is this by design?
This is how EF works. The problem is that your Playlist forms entity graph with other relations and EF uses very simple rule for tracking entity graphs: All entities in the graph must be tracked - there cannot be reference to entity which is not tracked. I don't give you reference to description of this rule, it is just my observation but I didn't find any single exception to this rule.
Edit: Updated version - I just checked internal implementation and relations are indeed nulled during calling Delete
So what happened in your code.
You marked your Playlist as deleted
EF passes delete operation to the state manager which does the fixup - it will null all relations
You saved changes to the database
Because there are no cascade deletes from Playlist all related objects remain undeleted
Once you saved changes EF internally accepted them and set change tracker to current state
Because the current state of Playlist is non existing (deleted in the database) it was detached from the context
Detaching has broken entity graph and EF fixed it by modifying navigation properties on both ends
The code responsible for nulling from System.Data.Objects.EntityEntry.Delete(doFixup) (doFixup is true) - the class is internal:
if (doFixup && (base.State != EntityState.Deleted))
{
this.RelationshipManager.NullAllFKsInDependentsForWhichThisIsThePrincipal();
this.NullAllForeignKeys();
this.FixupRelationships();
}
In your scenario this should have simple workaround - create DTO before you delete entity.