How to insert graph in Entity Framework with circular dependency - c#

This is how database structure looks like:
Vehicle has lot of CanNetworks and each CanNetwork has lot of ECUs. And that would save perfectly if that was only I have.
But, each vehicle also has one special ECU called gatewayECU so problem happens with saving because entity framework core does not know how to handle that scenario. It needs to insert vehicle before inserting ecus, but how to insert vehicle when ecu is not inserted.
This is what I tried: ignore (delete, invalidate) gatewayecu field (column is nullable in database), then I insert whole graph and then update vehicle with gatewayEcuId field I stored in some variable before doing anything.
Solution is not pretty. How to handle this scenario.
public class Vehicle : BaseEntity
{
public Vehicle()
{
CANNetworks = new List<CANNetwork>();
}
public List<CANNetwork>? CANNetworks { get; set; }
public ECU? GatewayECU { get; set; } = default!;
public int? GatewayECUId { get; set; }
}
public class CANNetwork : BaseEntity
{
public CANNetwork()
{
ECUs = new List<ECU>();
}
public string Name { get; set; } = default!;
public ICollection<ECU>? ECUs { get; set; }
public int VehicleId { get; set; }
public Vehicle? Vehicle { get; set; } = default!;
}
public class ECU : BaseEntity
{
public int CANNetworkId { get; set; }
public CANNetwork? CANNetwork { get; set; } = default!;
}
This is ugly solution which I don't want:
public async Task<int> Insert(Vehicle vehicleDefinition, ECU vehicleGatewayECU)
{
var result = -1;
using (var transaction = _databaseContext.Database.BeginTransaction())
{
result = await Insert(vehicleDefinition);
if (vehicleGatewayECU != null)
{
var ecu = await _databaseContext.ECUs.FirstOrDefaultAsync(x => x.Name == vehicleGatewayECU.Name && vehicleDefinition.Name == x.CANNetwork.Vehicle.Name);
if (ecu != null)
{
vehicleDefinition.GatewayECUId = ecu.Id;
result = await Update(vehicleDefinition);
transaction.Commit();
return result;
}
}
else
{
transaction.Commit();
}
}
return result;
}
EDIT:
I am now thinking about changing table structure in a way to get rid of gatewayECU field on Vehicle, and put some flag IsGatewayEcu in ECU table

Related

How should I update a table that is joined with another table in Entity Framework?

I have the following entities:
namespace SomeDataAccess
{
public partial class Patch
{
public int PatchID { get; set; }
public double Number { get; set; }
}
public partial class PatchFile
{
public int FileID { get; set; }
public int PatchID{ get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
}
And I have the following api model:
namespace Web_API.Models
{
[Table("SomeFiles")]
public class SomeFilesViewModel
{
[Key]
public int FileId { get; set; }
public int PatchNumber{ get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
}
The GET method is implemented successfully as following:
/ GET: api/SomeFiles/5
[ResponseType(typeof(SomeFileViewModel))]
public async Task<IHttpActionResult> GetSomeFileViewModel(int id)
{
var patchFile = await _context.PatchFile.FindAsync(id);
return someFile == null
? (IHttpActionResult)NotFound()
: Ok(new someFileViewModel
{
FileId = patchFile.FileID,
PatchNumber = patch.Number,
Name = patchFile.Name,
Type = patchFile.Type,
});
}
Thus far, I have implemented the PUT method as following:
// PUT: api/SomeFiles
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutSomeFileViewModel(SomeFilesViewModel someFileViewModel)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var file = new SomeDataAccess.PatchFile
{
FileID = someFileViewModel.FileId,
PatchID = _context.Patch.FirstOrDefault(i => i.Number == someFileViewModel.PatchNumber).PatchID
// How to get the relavent patch id by having the patch Number?
Name = someFileViewModel.Name,
Type = someFileViewModel.Type
};
_context.Entry(file).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!FileExists(file.FileID))
return NotFound();
throw;
}
return StatusCode(HttpStatusCode.NoContent);
}
And a sample payload:
Sample PayLoad:
{
"FileId" = 4
"PatchNumber" = 894
"Name" = "MyFile.exe"
Type = "Application"
}
How can I update or add a record to PatchFile entity if I only have the PatchNumber and not the PatchId to prevent conflicted with the FOREIGN KEY constraint?
_context.Patch.FirstOrDefault(i => i.Number == someFileViewModel.PatchNumber).PatchID
Is above the correct approach? If yes, Is this not making another trip to database? Is there a better approach?
You could add the PatchID to the SomeFilesViewModel along the PatchNumber. Otherwise there will be this extra query to the DB. On the other hand: this might create another possible problem, as the sent data don't have to be accurate and you'll need to check/validate it and that would be another trip to DB.
If you decide to stick with the extra query I would suggest rewriting it as following:
_context.Patch.Where(i => i.Number == someFileViewModel.PatchNumber).Select(i => i.PatchID).FirstOrDefault();
That way you get only the ID from the DB; assuming you don't need to work with other parts of your Patch object.

Error inserting record with entity framework

I am sorry if it has already been answered but I can't find any solution. Here is my (little) problem. Also all my apologies if the terms I use are approximate, I am far from being a skilled C# developer
Note that I think my problem is similar to this one Entity Framework validation error for missing field, but it's not missing?
I have a table "Tweets" with a tweet_id field (bigint) which is my primary key.
I use the following class to load the table :
class TwitterDbContext : DbContext
{
public TwitterDbContext() : base("Twitter")
{
}
public DbSet<Stream> Streams { get; set; }
public DbSet<StreamParameter> StreamParameters { get; set; }
public DbSet<Tweet> Tweets { get; set; }
}
public class Tweet
{
public Tweet()
{
}
[Key]
public long tweet_id { get; set; }
public string tweet { get; set; }
public long creator { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string language { get; set; }
public DateTime created_at { get; set; }
public DateTime registered_at { get; set; }
public long? in_reply_to { get; set; }
public bool retweeted { get; set; }
}
I have an other class to store within the code execution all the fields used by the Tweet table. For the need here, let's imagine I manually create it that way
private void Test_Button_Click(object sender, RoutedEventArgs e)
{
Twts twtReceived = new Twts();
twtReceived.tweet_id = 1;
twtReceived.tweet = "test";
twtReceived.creator = 1;
twtReceived.latitude = -1;
twtReceived.longitude = -1;
twtReceived.language = "a";
twtReceived.created_at = DateTime.Now;
twtReceived.registered_at = DateTime.Now;
twtReceived.in_reply_to = 1;
twtReceived.retweeted = true;
AddTweet(twtReceived);
}
Now here is the AddTweet method
static public void AddTweet(Twts twtReceived)
{
try
{
// update the tweet data in the database
using (var TwitterDb = new TwitterDbContext())
{
Tweet twt = new Tweet()
{
tweet_id = twtReceived.tweet_id,
tweet = twtReceived.tweet,
creator = twtReceived.creator,
longitude = twtReceived.longitude,
latitude = twtReceived.latitude,
language = twtReceived.language,
created_at = twtReceived.created_at,
registered_at = twtReceived.registered_at,
in_reply_to = twtReceived.in_reply_to,
retweeted = twtReceived.retweeted
};
TwitterDb.Tweets.Add(twt);
TwitterDb.SaveChanges();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
}
I constantly have the same error message:
Cannot insert the value NULL into column 'tweet_id', table
'Twitter.dbo.Tweets'; column does not allow nulls. INSERT fails.
The thing is that when I spy on "TwitterDb.Tweets.Local" after TwitterDb.Tweets.Add(twt); I correctly have tweet_id set to 1.
Any idea where is the issue?
Try marking your tweet_id field with following (instead of just [Key]), if this is a primary key column where you want to provide values yourself
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
If it is an auto-increment, then remove explicit assignments to this field and mark it as 'Identity' instead:
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

Entity Framework: Get the underlying entity type when foreign key changes for audit trail

I'm currently working on a project where the requirement is to log changes to each individual property of an entity.
Many of the entity's have 1 to many relationship with a lookup value type and when this changes I can so far track that i.e. '1' was changed to '2'.
What I'm trying to accomplish is to translate the number to a more readable value.
I have the below bit of code which I thought was going to work but it doesn't :(
namespace DomainClasses
{
public interface IAuditDescribable
{
String GetAuditDescription();
}
public abstract class BaseEntity
{
public Int32 Id { get; set; }
}
public class Car : BaseEntity
{
public String Vrn { get; set; }
public Int32? TransmissionId { get; set; }
public virtual Transmission Transmission { get; set; }
}
public class Transmission : BaseEntity, IAuditDescribable
{
public String Name { get; set; }
public String GetAuditDescription()
{
return Name;
}
}
}
namespace AuditTrailTwo
{
public class AuditContext : DbContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Transmission> Transmissions { get; set; }
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
var octx = ((IObjectContextAdapter)this).ObjectContext;
var changes = octx.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
foreach (var change in changes)
{
var changedProperties = change.GetModifiedProperties();
var associationSets = change.EntitySet.EntityContainer.AssociationSets;
foreach (var changedProperty in changedProperties)
{
CurrentValueRecord current = change.CurrentValues;
var newValue = current.GetValue(
current.GetOrdinal(changedProperty));
DbDataRecord original = change.OriginalValues;
var oldValue = original.GetValue(
original.GetOrdinal(changedProperty));
var aSet = associationSets
.Where(x => x.ElementType.IsForeignKey)
.FirstOrDefault(x => x.ElementType.Constraint.ToProperties[0].Name == changedProperty);
if (aSet != null)
{
var targetEnd = aSet.AssociationSetEnds.GetValue(aSet.Name+"_Target", true);
//This type is always null because the 'FullName' returns: 'AuditTrailTwo.Transmission'
//and not 'DomainClasses.Transmission' as I was expecting.
var type = Type.GetType(targetEnd.EntitySet.ElementType.FullName);
if (type != null)
{
var typeSet = Set(type);
var newEntry = typeSet.Find(newValue) as IAuditDescribable;
var newEntryValue = newEntry.GetAuditDescription();
var oldEntry = typeSet.Find(oldValue) as IAuditDescribable;
var oldEntryValue = oldEntry.GetAuditDescription();
}
}
}
}
return base.SaveChanges();
}
}
}
I have looked at many posts discussing auditing with entity framework but I have so far not found any way to achieve what I'm trying to do. I'm hoping it's simply because I'm overlooking something.
Any help is appreciated.
You could use aspect oriented programming to handle when a property has changed.
http://www.postsharp.net/

EF adding duplicate records into lookup/reference table

I have 3 tables,
1. AttributeTypes (Columns: AttributeId (PK), AttributeName, ..)
2. Location (Columns: locationId (PK), LocationName, ...)
3. LocationAttributeType (Columns: locationId (FK), AttributeId (FK))
Whenever I am trying to insert new location record along with its attribute type from GUI, it should create new record for Table- Location and LocationAttributeType. But EF trying to add new record in Table- AttributeTypes as well, which is just used as reference table and should not add new/duplicate records in it. How can I prevent that?
here is my code,
The model which GUI sends is,
public class LocationDataModel
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Code { get; set; }
[DataMember]
public List<AttributeTypeDataModel> AssignedAttributes = new List<AttributeTypeDataModel>();
}
public class AttributeTypeDataModel
{
protected AttributeTypeDataModel() {}
public AttributeTypeDataModel(int id) { this.Id = id; }
public AttributeTypeDataModel(int id, string name)
: this(id)
{
this.Name = name;
}
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public virtual ICollection<LocationDataModel> Locations { get; set; }
}
The Entities created by EF are,
public partial class Location
{
public Location()
{
this.AttributeTypes = new List<AttributeType>();
}
public Location(int campusId, string code)
: this()
{
CampusId = campusId; Code = code;
}
public int Id { get; set; }
public int CampusId { get; set; }
public string Code { get; set; }
public virtual ICollection<AttributeType> AttributeTypes { get; set; }
}
public partial class AttributeType
{
public AttributeType()
{
this.Locations = new List<Location>();
}
public int AttributeTypeId { get; set; }
public string AttributeTypeName { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
I have below code to Add these new location to database,
private IEnumerable<TEntity> AddEntities<TModel, TEntity, TIdentityType>
(IEnumerable<TModel> models, Func<TModel, TIdentityType> primaryKey,
IGenericRepository<TEntity, TIdentityType> repository)
{
var results = new List<TEntity>();
foreach (var model in models)
{
var merged = _mapper.Map<TModel, TEntity>(model);
var entity = repository.Upsert(merged);
results.Add(entity);
}
repository.Save();
return results.AsEnumerable();
}
I am using following generic repository to do entity related operations
public TEntity Upsert(TEntity entity)
{
if (Equals(PrimaryKey.Invoke(entity), default(TId)))
{
// New entity
return Context.Set<TEntity>().Add(entity);
}
else
{
// Existing entity
Context.Entry(entity).State = EntityState.Modified;
return entity;
}
}
public void Save()
{
Context.SaveChanges();
}
Whats wrong I am doing here?
The DbSet<T>.Add() method attaches an entire object graph as added. You need to indicate to EF that the 'reference' entity is actually already present. There are two easy ways to do this:
Don't set the navigation property to an object. Instead, just set the corresponding foreign key property to the right value.
You need to ensure that you don't load multiple instances of the same entity into your object context. After creating the context, load the full list of AttributeType entities into the context and create a Dictionary<> to store them. When you want to add an attribute to a Location retrieve the appropriate attribute from the dictionary. Before calling SaveChanges() iterate through the dictionary and mark each AttributeType as unchanged. Something like this:
using (MyContext c = new MyContext())
{
c.AttributeTypes.Add(new AttributeType { AttributeTypeName = "Fish", AttributeTypeId = 1 });
c.AttributeTypes.Add(new AttributeType { AttributeTypeName = "Face", AttributeTypeId = 2 });
c.SaveChanges();
}
using (MyContext c = new MyContext())
{
Dictionary<int, AttributeType> dictionary = new Dictionary<int, AttributeType>();
foreach (var t in c.AttributeTypes)
{
dictionary[t.AttributeTypeId] = t;
}
Location l1 = new Location(1, "Location1") { AttributeTypes = { dictionary[1], dictionary[2] } };
Location l2 = new Location(2, "Location2") { AttributeTypes = { dictionary[1] } };
// Because the LocationType is already attached to the context, it doesn't get re-added.
c.Locations.Add(l1);
c.Locations.Add(l2);
c.SaveChanges();
}
In this specific case you are using a many-to-many relationship, with EF automatically handling the intermediate table. This means that you don't actually have the FK properties exposed in the model, and my first suggestion above won't work.
Therefore, you either need to use the second suggestion, which still ought to work, or you need to forgo the automatic handling of the intermediate table and instead create an entity for it. This would allow you to apply the first suggestion. You would have the following model:
public partial class Location
{
public Location()
{
this.AttributeTypes = new List<LocationAttribute>();
}
public Location(int campusId, string code)
: this()
{
CampusId = campusId; Code = code;
}
public int Id { get; set; }
public int CampusId { get; set; }
public string Code { get; set; }
public virtual ICollection<LocationAttribute> AttributeTypes { get; set; }
}
public partial class LocationAttribute
{
[ForeignKey("LocationId")]
public Location Location { get; set; }
public int LocationId { get; set; }
public int AttributeTypeId { get; set; }
}
public partial class AttributeType
{
public int AttributeTypeId { get; set; }
public string AttributeTypeName { get; set; }
}
With this approach you do lose functionality since you can't navigate from a Location to an AttributeType without making an intermediate lookup. If you really want to do that, you need to control the entity state explicitly instead. (Doing that is not so straightforward when you want to use a generic repository, which is why I've focused on this approach instead.)
Thank you all for your suggestions.
I have to get rid of my generic repository here to save my context changes and do it manually as below,
private IEnumerable<int> AddLocationEntities(IEnumerable<LocationDataModel> locations)
{
var results = new List<int>();
foreach (LocationDataModel l in locations)
{
var entity = _mapper.Map<LocationDataModel, Location>(l);//you can map manually also
var AttributeCode = l.AssignedAttributes.FirstOrDefault().AttributeTypeId;
using (MyContext c = new MyContext())
{
var attr = c.AttributeTypes.Where(a => a.Id == AttributeTypeId ).ToList();
entity.AttributeTypes = attr;
c.Locations.Add(entity);
c.SaveChanges();
var locid = entity.Id;
results.Add(locid);
}
}
return results;
}
In the else statement of yourUpsert you should add
context.TEntity.Attach(entity);

Update multiple tables in MVC Edit Action using repository

I have a pair of ViewModels that references data from a number of tables. One for displaying and one for editing.
When I return data from the display ViewModel I can map all the relevant fields using ValueInjecter InjectFrom functionality.
What do I do next to get the database to update?
If I send the models to my Update method in the repository I can see the changes in the model but the context doesn't pick them up. Am I missing a step or is there a better way of doing this?
If I try to modify one table at a time I can get the context to pick up the changes but then get an error as follows:
Store update, insert, or delete statement affected an unexpected
number of rows (0).
---EDIT---
I've updated the code and moved the mapping into the repository but I'm still getting the same error even though the debugger shows the entities with the new values.
ViewModels
public partial class HouseholdEditViewModel //for displaying in browser
{
public int entityID { get; set; }
public int familyID { get; set; }
public string UPRN { get; set; }
public string address { get; set; }
public HousingTypeDropDownViewModel housingTypeID { get; set; }
public KeyworkerDropDownViewModel keyworkerID { get; set; }
public string startDate { get; set; }
public bool loneParent { get; set; }
public string familyPhoneCode { get; set; }
public string familyPhone { get; set; }
}
public partial class HouseholdAddViewModel //for mapping to database
{
public int familyID { get; set; }
public string UPRN { get; set; }
public string address { get; set; }
public int entityTypeID { get; set; }
public int housingTypeID { get; set; }
public int keyworkerID { get; set; }
public DateTime startDate { get; set; }
public bool loneParent { get; set; }
public string familyPhoneCode { get; set; }
public string familyPhone { get; set; }
}
Repository (Current version - I've attempted a few different things without success)
public interface IHouseholdRepository : IDisposable
{
//other methods here...
void Update(HouseholdAddViewModel model, int id);
}
public void Update(HouseholdAddViewModel model, int id)
{
//check address exists
var address = (from u in context.tAddress
where model.UPRN.Contains(u.UPRN)
select u.UPRN);
var ae = new tAddressEntity();
ae.InjectFrom(model);
ae.entityID = id;
ae.UPRN = model.UPRN;
context.tAddressEntity.Attach(ae);
context.Entry(ae).State = EntityState.Modified;
var e = new tEntity();
e.InjectFrom(model);
e.entityID = id;
e.entityName = model.address;
e.tAddressEntity.Add(ae);
context.tEntity.Attach(e);
context.Entry(e).State = EntityState.Modified;
var a = new tAddress();
a.InjectFrom(model);
context.tAddress.Attach(a);
context.Entry(a).State = address.ToString() == string.Empty ?
EntityState.Added :
EntityState.Modified;
var hs = new tHousingStatus();
hs.InjectFrom(model);
hs.entityID = id;
context.tHousingStatus.Attach(hs);
context.Entry(hs).State = EntityState.Modified;
var k = new tKeyWorker();
k.InjectFrom(model);
k.entityID = id;
context.tKeyWorker.Attach(k);
context.Entry(k).State = EntityState.Modified;
var l = new tLoneParent();
l.InjectFrom(model);
l.entityID = id;
context.tLoneParent.Attach(l);
context.Entry(l).State = EntityState.Modified;
var h = new tHousehold();
h.InjectFrom(model);
h.entityID = id;
h.tHousingStatus.Add(hs);
h.tKeyWorker.Add(k);
h.tLoneParent.Add(l);
context.Entry(h).State = EntityState.Modified;
context.SaveChanges();
}
Controller
[HttpPost]
public ActionResult Edit(HouseholdAddViewModel model, int id)
{
model.entityTypeID = _repo.GetEntityType();
if (ModelState.IsValid)
{
_repo.Update(model, id);
return RedirectToAction("Index");
}
return View("Edit", id);
}
The easiest way to update an entity using EF is to retrieve the entity (using
it's key) and then apply the updates to that object instance. EF will automatically detect the updates to the entity and apply them when you call SaveChanges().
It seems as if you're creating new entities and you're not adding them to context so they
aren't being picked up.
I would change your Edit controller to do this
[HttpPost]
public ActionResult Edit(HouseholdAddViewModel model, int id)
{
model.entityTypeID = _repo.GetEntityType();
if (ModelState.IsValid)
{
var h = _repo.GetHousehold(id);
h.InjectFrom(model);
h.entityID = id;
//...
}
}

Categories

Resources