Audit.NET Entity Framework to audit one same entry to two tables - entity-framework

I Have 2 Audit tables one is Audit_ProfileFan and second is Audit_StatusChanges
The first table AuditProfileFan should audit every time update or insert has been made to ProfileFan Table.
The second Audit_StatusChanges should only audit when an update of a certain column FanStatusId is made.
Audit.Core.Configuration.Setup() .UseEntityFramework(ef => ef.AuditTypeExplicitMapper(m => m
.Map<FanActivity, Audit_FanActivity>((fanActivity, auditFanActivity) =>
{
auditFanActivity.ProfileFanId = fanActivity.ProfileFanId;
auditFanActivity.ActivityId = auditFanActivity.ActivityId;
})
.Map<DeliveryActions, Audit_DeliveryActions>((deliveryAction, auditDeliveryAction) =>
{
auditDeliveryAction.ProfileFanId = deliveryAction.FanId;
auditDeliveryAction.DeliveryActionId = deliveryAction.DeliveryActionId;
})
.Map<Fan, Audit_Fan>()
.Map<ProfileFan, Audit_StatusChanges>((profileFan, auditStatusChanges) =>
{
auditStatusChanges.ProfileFanId = profileFan.Id;
//auditStatusChanges.OriginalValue = profileFan.FanStatusId;
//auditStatusChanges.NewValue = profileFan.FanStatusId;
})
.Map<ProfileFan, Audit_ProfileFan>((profileFan, auditProfileFan) =>
{
auditProfileFan.ProfileFanId = profileFan.Id;
auditProfileFan.FanId = profileFan.FanId;
auditProfileFan.EmailResponseStatusId = profileFan.EmailResponseStatusId;
auditProfileFan.FanStatusId = profileFan.FanStatusId;
})
.Map<TagFan, Audit_TagFan>((tagFan, auditTagFan) =>
{
auditTagFan.ProfileFanId = tagFan.ProfileFanId;
auditTagFan.TagId = tagFan.TagId;
})
.AuditEntityAction<IAuditLog>((evt, entry, auditEntity) =>
{
if(entry.Table=="ProfileFan" && entry.Action=="Update")
{
//auditEntity.OriginalValue = profileFan.FanStatusId;
//auditEntity.NewValue = profileFan.FanStatusId;
}
auditEntity.AuditDate = DateTime.Now;
auditEntity.AuditUser = evt.Environment.UserName;
auditEntity.Action = entry.Action; // Insert, Update, Delete
auditEntity.AuditUsername = HttpContext.Current.User.Identity.Name;
})
)
);
But every time an update is made it audits only one table in this case Audit_ProfileFan.
Is my requirement possible or should I do some kind of workaround?

This is not possible with the current version of the EntityFramework data provider, since you can only map from the known entity type and nothing else.
But I've found a way to allow that kind of use cases with minimal impact, by adding a new Map<T> overload that lets you specify the final audit type as a function of the EventEntry, so you would be able to map the same input data type to multiple output audit types, depending on the modified entry.
So for example you could map ProfileFan to different tables depending on the SQL operation (insert/update), with something like this:
Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef.AuditTypeExplicitMapper(m => m
.Map<ProfileFan>(
mapper: entry => entry.Action == "Insert" ? typeof(Audit_ProfileFan) : typeof(Audit_StatusChanges),
entityAction: (ev, entry, entity) =>
{
if (entity is Audit_ProfileFan pf)
{
// action for profile fan
// pf.xxxx = ...;
}
else if (entity is Audit_StatusChanges ss)
{
// action for status changes
// ss.xxxx = ...;
}
})
.Map<TagFan, Audit_TagFan>(/*...*/)
.AuditEntityAction<IAuditLog>((evt, entry, auditEntity) =>
{
// common action...
})));
This will be released soon, here is the commit with the changes.
UPDATE
This is included on Audit.EntityFramework library starting on version 14.6.2

Related

Update a BuiltList from built_value

I want to update the BuiltList in the state if some condition is met. Here is my reducer
TravelDeductionsStateBuilder _travelDeductionBreakfastToggled(
TravelDeductionsStateBuilder state,
TravelDeductionBreakfastToggledAction action) =>
state
..days = state.days.build().rebuild((d) {
if (d[action.key].deductions.contains(TravelDeductionType.BREAKFAST)) {
d[action.key]
.deductions
.rebuild((p0) => p0.remove(TravelDeductionType.BREAKFAST));
} else {
d[action.key]
.deductions
.rebuild((p0) => p0.add(TravelDeductionType.BREAKFAST));
}
}).toBuilder();
I need to add/remove ENUM entry into deductions but it seems that this update is ignored and I am not able to update this list accordingly. Any tips of what may be wrong?
If anyone wonders this is the solution
TravelDeductionsStateBuilder _travelDeductionBreakfastToggled(
TravelDeductionsStateBuilder state,
TravelDeductionBreakfastToggledAction action) =>
state
..days = state.days.build().rebuild((d) {
if (d[action.index]
.deductions
.contains(TravelDeductionType.BREAKFAST)) {
d[action.index] = d[action.index].rebuild(
(b) => b.deductions.remove(TravelDeductionType.BREAKFAST));
} else {
d[action.index] = d[action.index]
.rebuild((b) => b.deductions.add(TravelDeductionType.BREAKFAST));
}
}).toBuilder();
You can find the explanation here https://github.com/google/built_collection.dart/issues/259#issuecomment-1122390601

EF Core entity refresh

I would like if there is a way to trigger an object hierachy's lazy load after it has been added in the cache following an insert.
If I insert an object only providing it the foreign keys ids and try to request it right after, none of these properties / collections are retrieved.
Example:
_dbContext.Set<MYTYPE>().Add(myObject);
await _dbContext.SaveChangesAsync(ct);
_dbContext.Entry(myObject).Collection(c => c.SomeCollection).Load();
_dbContext.Entry(myObject).Reference(c => c.Property1).Load();
_dbContext.Entry(myObject.Property1).Reference(c => c.NestedProperty).Load();
_dbContext.Entry(myObject).Reference(c => c.Property2).Load();
_dbContext.Entry(myObject).Collection(c => c.SomeCollection2).Load();
_dbContext.Entry(myObject).Reference(c => c.Property3).Load();
_dbContext.Entry(myObject).Reference(c => c.Property4).Load();
_dbContext.Entry(myObject.Property4).Reference(c => c.SomeOtherNestedProperty).Load();
_dbContext.Entry(myObject).Reference(c => c.Property5).Load();
_dbContext.Entry(myObject).Reference(c => c.Property6).Load();
return _dbContext.Set<MYTYPE>().Where(x => x.Id == myObject.Id);
Unless I do all the Load() (having to nest into several layers sometimes ...), I can't have any of the sub properties properly lazy loaded.
Note: doing
_dbContext.Entry(myObject).Reload()
or
_dbContext.Entry(myObject).State = EntityState.Detached;
doesn't work.
Any idea? Because I have several cases like this, with HEAVY object nesting, and it feels really bad to have to do all the loads manually.
Thanks.
The thing is, if you try to add some line using only the integer as a foreign key like below :
db.tablename.Add(new tablename{
name = "hello",
FK_IdofanotherTable = 5
});
db.SaveChanges();
Then entity does not care about linking the object immediately, you won't be able to access the linked object as a property.
This is how it's actually done in modern Entity Framework :
var TheActualCompleteObject = db.AnotherTable.First(t => t.id == 5);
db.tablename.Add(new tablename{
name = "hello",
AnotherTable = TheActualCompleteObject
});
db.SaveChanges();
So... basically, you will have loaded the object anyway, it's not gonna make anything faster, but it will be more consistant. You will be able to navigate through foreign keys properties.
If this is not possible, then you will have to query back AFTER SaveChanges to retrieve your object AND have access to foreign properties (eagerly loading them, of course, or else it's the same disaster)
Exemple :
_dbContext.Configuration.LazyLoadingEnabled = false;
_dbContext.Set<MYTYPE>().Add(myObject);
await _dbContext.SaveChangesAsync(ct);
var v = _dbContext.First(t => t.id == myObject.id);
var fk_prop = v.Property1;
I also found no easy way around this.
The following might make it simpler...
public static void Reset(this DbContext context)
{
var entries = context.ChangeTracker
.Entries()
.Where(e => e.State != EntityState.Unchanged)
.ToArray();
foreach (var entry in entries)
{
switch (entry.State)
{
case EntityState.Modified:
entry.State = EntityState.Unchanged;
break;
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Deleted:
entry.Reload();
break;
}
}
}
Hope this helps!
JS

EF. How to include only some sub results in a model?

I'm trying to select list of Users and for each User JobTitle in correct language depended of strLang selected by user.
Something like that:
IList<User> myData;
myData = Context.Users.Where(u => u.Location == strLocation)
.Include(u => u.JobTitles.Where(e => e.Language == strLang))
.ToList();
But it seems Include doesn't like Where clause
You can't conditionally include only a few entities of a related collection, so you should use projection to get the stuff you need:
IList<User> myData;
var temp = Context.Users.Where(u => u.Location == strLocation)
.Select(u => new
{
User = u;
Locations = u.JobTitles.Where(e => e.Language == strLang));
});
foreach(var t in temp)
{
User user = t.User;
user.Locations = t.Locations;
myData.Add(user);
}
You cannot do it by using the "Include" method since it only take naviation properties.
Disclaimer: I'm the owner of the project EF+ on github.
EF+ Query IncludeFilter allow you to easily filter related entities:
// using Z.EntityFramework.Plus; // Don't forget to include this.
IList<User> myData;
myData = Context.Users.Where(u => u.Location == strLocation)
.IncludeFilter(u => u.JobTitles.Where(e => e.Language == strLang))
.ToList();
You can find the project here
You can find the documentation here
Behind the code, IncludeFilter do exactly like Alexander answer by using a projection.

declare variable to store linq entity for conditional statements

I am trying to look up record using if I have the key then use Find if not use Where
private ApplicationDbContext db = new ApplicationDbContext();
public bool DeactivatePrice(int priceId = 0, string sponsorUserName = "")
{
var prices = db.BeveragePrices;
// if we have an id then find
if (priceId != 0)
{
prices = prices.Find(priceId);
}
else
{
prices = prices.Where(b => b.UserCreated == sponsorUserName);
}
if (prices != null)
{
// do something
}
return true;
I get the following error for
prices = prices.Find(priceId);
Cannot convert app.Model.BeveragePrices from system.data.entity.dbset
I am copying the pattern from this answer but something must be different.
Seems you forgot to put a predicate inside the Find function call. Also you need to do ToList on the collection. The second option is a lot more efficient. The first one gets the whole collection before selection.
Another note commented by #Alla is that the find returns a single element. So I assume another declaration had been made for 'price' in the first option I state down here.
price = prices.ToList.Find(b => b.PriceId == priceId);
Or
prices = prices.Select(b => b.PriceId == priceId);
I assume the field name is PriceId.

"Unable to create a constant value of type .. Only primitive types are supported ..'' in EF query?

I have a work around for this issue, however I would appreciate it if someone could explain why this is happening and how I would design this for large datasets where my work around would not be viable.
The full error is:
Unable to create a constant value of type 'THPT_Razor.Models.WinType'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
and I am using EF v4.0.
The commented lines are the offending code and the work around is the "For loop"
Thank you in advance.
List<WinType> _atype = db.WinTypes.Where(wt => wt.IsWin == false).ToList();
List<WinType> _wtype = db.WinTypes.Where(wt => wt.IsWin == true).ToList();
string test = _wtype.Where(wt => wt.Value ==0).Select(wt => wt.Description).SingleOrDefault();
List<WinCheckDetails> wcd = db.Wins.Include("UserProfiles").Where(w => w.venueLogId == logid).Select(w => new WinCheckDetails
{
//awarddesc = w.atypeid.HasValue ? _atype.Where( wt=> wt.Value == w.atypeid).Select(wt => wt.Description).SingleOrDefault():string.Empty,
//windesc = _wtype.Where(wt => wt.Value == w.typeid).Select(wt => wt.Description).Single(),
atypeid = w.atypeid,
typeid = w.typeid,
WinId = w.WinId,
other = w.other,
posterid = w.posterid,
confirmed = w.confirmed,
posttime = w.posttime,
game = w.game,
playerid = w.UserProfile.PlayerID,
firstname = w.UserProfile.FirstName,
lastname = w.UserProfile.LastName,
fullname = w.UserProfile.FirstName + " " + w.UserProfile.LastName
}).OrderBy(o => o.game).ToList();
foreach (WinCheckDetails wc in wcd)
{
wc.awarddesc = _atype.Where(wt => wt.Value == wc.atypeid).Select(wt => wt.Description).SingleOrDefault();
wc.windesc = _wtype.Where(wt => wt.Value == wc.typeid).Select(wt => wt.Description).SingleOrDefault();
}
_atype and _wtype are lists of WinType in memory because you are applying ToList() to the queries. With respect to database queries they are collections of constant values because to perform the query in the database they have to be transmitted to the database server as the values they are in memory. EF doesn't support to transfer such constant values or collections of values from memory to the database unless they are values of primitive types (int for example). That's the reason why you get an exception.
Did you try to use _atype and _wtype as IQueryable instead of lists:
IQueryable<WinType> _atype = db.WinTypes.Where(wt => !wt.IsWin);
IQueryable<WinType> _wtype = db.WinTypes.Where(wt => wt.IsWin);
List<WinCheckDetails> wcd = db.Wins
.Where(w => w.venueLogId == logid)
.Select(w => new WinCheckDetails
{
awarddesc = w.atypeid.HasValue
? _atype.Where(wt=> wt.Value == w.atypeid)
.Select(wt => wt.Description).FirstOrDefault()
: string.Empty,
windesc = _wtype.Where(wt => wt.Value == w.typeid)
.Select(wt => wt.Description).FirstOrDefault(),
// ... (unchanged)
}).OrderBy(o => o.game).ToList();
I have removed the Include because it will be ignored anyway when you perform a projection with Select. Also I have replaced SingleOrDefault and Single by FirstOrDefault because both are not supported in a projection (and First neither), only FirstOrDefault is supported.
I am not sure if that will work. But it should remove your exception (but maybe you'll get another one...).