I'm new to using EF to handle data in SQL. In a MVC Core project we're testing EF (Microsoft.EntityFrameworkCore, version 2.2.3) to handle data.
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
Code for updating:
public void Update(Contact contact)
{
_dbContext.Update(contact);
_dbContext.SaveChanges();
}
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
It should not.
test it, try to debug here
_dbContext.Update(contact);
_dbContext.SaveChanges();
var updated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id); //debug here
check if it has a value, if still none, these are the scenarios i can think of that may have caused your problem
investigate the missing field specially if it is not nullable.
is the _dbContext used here is the same connection string used with everything?
is the [Key] attribute listed on your Contact entity?
public class Contact
{
[Key]
public int Id
}
overridden the SaveChanges function?
is what you are passing Contact contains a Key and it is not 0?
is a delete function called after Update?
try using SQL Profiler to look at the Linq to SQL if it really generated an update query and if it is really pointing at the right [Key]
but if it is still not working properly, you could do
public void Update(Contact contact)
{
var selectedContactToBeUpdated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id);
if (selectedContactToBeUpdated != null)
{
selectedContactToBeUpdated.PropertyToBeUpdated1 = newValue;
selectedContactToBeUpdated.PropertyToBeUpdated2 = newValue2;
//additional Properties
_dbContext.SaveChanges();
}
}
in the scenario above, it will only generate an Update statement with fields you have changed.
Related
I used json serialization to store list on ids in a field
Model:
public class Video
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IList<int> AllRelatedIds { get; set; }
}
Context:
modelBuilder.Entity<Video>(entity =>
{
entity.Property(p => p.AllRelatedIds).HasConversion(
v => JsonConvert.SerializeObject(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
v => JsonConvert.DeserializeObject<IList<int>>(v, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })
);
});
It works fine, Adding, Editing, Deleting items is easy and in SQL Database it stores as json like
[11000,12000,13000]
Everything is fine BUT!! as soon as want to query on this list I get weird responses.
Where:
_context.Set<Video>().Where(t=>t.AllRelatedIds.contains(11000)) returns null however if I ask to return all AllRelatedIds items some records have 11000 value exp.
Count:
_context.Set<Video>().Count(t=>t.AllRelatedIds.contains(11000)) returns could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
What's the matter with EF Core? I even tested t=>t.AllRelatedIds.ToList().contains(11000) but made no difference
What I should do? I don't want to have more tables, I used this methods hundreds of times but seems never queried on them.
The Json Serialization/Deserialization happens at application level. EF Core serializes the IList<int> object to value [11000,12000,13000] before sending it to database for storing, and deserializes the value [11000,12000,13000] to IList<int> object after retrieving it from the database. Nothing happens inside the database. Your database cannot operate on [11000,12000,13000] as a collection of number. To the database, its a single piece of data.
If you try the following queries -
var videos = _context.Set<Video>().ToList();
var video = _context.Set<Video>().FirstOrDefault(p=> p.Id == 2);
you'll get the expected result, EF Core is doing it's job perfectly.
The problem is, when you query something like -
_context.Set<Video>().Where(t=> t.AllRelatedIds.Contains(11000))
EF Core will fail to translate the t.AllRelatedIds.Contains(11000) part to SQL. EF Core can only serialize/deserialize it because you told it to (and how). But as I said above, your database cannot operate on [11000,12000,13000] as a collection of integer. So EF Core cannot translate the t.AllRelatedIds.Contains(11000) to anything meaningful to the database.
A solution will be to fetch the list of all videos, so that EF Core can deserialize the AllRelatedIds to IList<int>, then you can apply LINQ on it -
var allVideos = _context.Set<Video>().ToList();
var selectedVideos = allVideos.Where(t=> t.AllRelatedIds.Contains(11000)).ToList();
But isn't fetching ALL videos each time unnecessary/overkill or inefficient from performance perspective? Yes, of course. But as the comments implied, your database design/usage approach has some flaws.
I am developing code for app engine. I tried to update an existing row by updating the same entity returned as result of query. But it creates new row instead of updating the same row. Following is the code:
public boolean updateProfile(DbProfile profile) {
Transaction txn = _datastore.beginTransaction();
Entity entity = getProfileEntity(profile.getLoginId());
if (entity != null) {
entity.setProperty(DbProfile.DbProfilePropertyNames.address, profile.getAddress());
entity.setProperty(DbProfile.DbProfilePropertyNames.name, profile.getName());
Key key = _datastore.put(entity);
txn.commit();
return true;
}
return false;
}
private Entity getProfileEntity(String userName) {
Key eRecommendationKey = KeyFactory.createKey("eRecommendation", _dbKeyName);
FilterPredicate predicateUsername =
new FilterPredicate(DbProfile.DbProfilePropertyNames.loginId, FilterOperator.EQUAL,
userName.toUpperCase());
Query query =
new Query(DbProfile.entityProfileName, eRecommendationKey).setFilter(predicateUsername);
List<Entity> profiles =
_datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
Utils.log.log( Level.SEVERE, "not found"+profiles.size() );
if (profiles.size() == 0) {
//profile data is not set yet
return null;
} else {
return profiles.get(0);
}
}
Following image shows fields in the entity.
Please let me know how can I fix the issue.
My Java skills are not too good, so I find it difficult to understand your code sample. I also don't see where updateProfile() is called and how your code is getting or constructing the profile object, especially whether the key of the profile object is altered.
But in general, if new entities are created instead of updating existing entities, the reason is that the key at your updating commit is different from the actual key of the existing entity.
Typical situations:
a string (key-name) is used instead of an integer (ID), or vice versa
a typo in the kind name of the key
different namespace or app properties of the key
parents are missing in the key path or are constructed wrongly
Suggestion:
In datastore viewer, compare the key of an existing entity with the key of the accidentally created entity. The difference between both keys might give you a hint where to look in your code for the bug.
I solved the problem. It was my mistake. I had called saveData servlet instead of updateProfile servlet.
I have a structure like this
DRDLines:
ID
DrawingRevisionID
DrawingRevision:
ID
Name
They're related in a one-to-many relationship.
In this code example
DRDLine line;
using (var db = new AMPX_DCEntities())
{
line = db.DRDLines.Single(p => p.ID == 1);
System.Console.WriteLine(line.DrawingRevision.ID);
}
using (var db = new AMPX_DCEntities())
{
var id = 12;
line.DrawingRevisionID = id;
}
using (var db = new AMPX_DCEntities())
{
db.Entry(line).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
I get this error
A referential integrity constraint violation occurred: The property value(s) of 'DrawingRevision.ID' on one end of a relationship do not match the property value(s) of 'DRDLine.DrawingRevisionID' on the other end.
What I've found: it doesn't update relations in DRDLines inside DrawingRevision
Debugging I see:
line.DrawingRevision.DRDLines[0].ID != line.DrawingRevisionID
If I remove line
System.Console.WriteLine(line.DrawingRevision.ID);
or write it like this
System.Console.WriteLine(line.DrawingRevisionID);
everything goes without errors. But I need that line to be used.
So, how can I fix that?
My guess is that the problem is caused by repeatedly creating a new context and then disposing it. When you set the DrawingRevisionID here
using (var db = new AMPX_DCEntities())
{
var id = 12;
line.DrawingRevisionID = id;
}
line is detached from the dbcontext from which it was retreived, but isn't attached to the new DbContext you've created, hence EF won't wire up the relationships when you change the ID.
You could attach the line object back to the context before changing the ID
db.DRDLines.Attach(line);
That will change both the IDs (although you could just change the other ID manually). Since that context is then disposed, you may need to set the EntityState to Modified for the DrawingRevision (or at least of the ID property) in the last DbContext session.
Also, I would add an Include to the original query to eagerly load the DrawingRevision. At the moment its only loaded when you query the ID on the System.Console line, hence why the behaviour is different. This also causes an extra trip to the database. Putting it into an include will be more efficient and more predictable.
I'm trying to update an entity using Session.Update then continue to execute another SQL query. The other query did not see the changed value. When i traced it using profiler, Session.Update did nothing.
public class InvoiceService()
{
public void Update(Invoice invoice)
{
using (var trans = BeginTransaction())
{
Session.Update(invoice); //Nhibernate did not update invoice.
ExecuteNamedQuery(); //Query executed before invoice updated.
trans.Commit(); //Invoice updated.
}
}
}
Then i add Session.Flush after Session.Update.
using (var trans = BeginTransaction())
{
Session.Update(invoice);
Session.Flush()
ExecuteNamedQuery();
trans.Commit();
}
After Session.Flush executed, SQL query for update is executed also.
It works perfectly. The execution order is correct. But then i executed another method to get all invoices. Committing transaction makes nhibernate execute update query to update my updated invoice earlier with old values. (ex: Quantity = 20, updated to 10, then updated again to 20)
public void FindAll()
{
using (var trans = BeginTransaction())
{
var invoices = Session.CreateCriteria<Invoice>().List<Invoice>();
trans.Commit(); // In here invoice that i updated earlier get updated again, using old values.
return invoices;
}
}
Why it's getting updated again?
What's the solution for this problem?
Thanks in advance.
Update is an unfortunate name for the method; the purpose of Update is to attach a transient instance to a new session. See the documentation for update and make sure you understand instance states.
The invoice is updated to the original values because NHibernate thinks it has changed. This "phantom" update may be caused by a property changing unexpectedly. A typical root cause is a nullable database column mapped to a non-nullable property (or vice-versa). The easiest way to troubleshoot is to turn on dynamic-update in the session factory configuration so that you can see which properties NHibernate detects as dirty.
I've got a one-to-many relationship set up. (Ex. A Person with many Phone Numbers). In my get query i have this.ObjectContext.Person.Include("PhoneNumbers") and the in generated MetaData including public EntityCollection<PhoneNumbers> PhoneNumbers{ get; set; } I have also set up a DTO with this and other properties i need.
[Include]
[Association("Name","thisKey","otherKey")]
public IEnumerable<PhoneNumbers> PNums { get; set; }
I can retrieve all the data alright, and display it in silverlight, but when I create a new one I run into problems. I've got this kind of thing going on:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (dgMMs.SelectedItem != null)
{
PhoneNumbers wb = new PhoneNumbers ();
wb.this = tbThis.Text;
wb.that = tbThat.Text;
wb.other = tbOther.Text;
wb.whatnot = tbwhatnot.Text;
((Person)dgMMs.SelectedItem).PNums.Add(wb);
}
}
Then I get this error when calling TDataSource.SubmitChanges();:
Message = "Submit operation failed
validation. Please inspect
Entity.ValidationErrors for each
entity in EntitiesInError for more
information."
Alright, So i did that, and sure enough there is an error, but I don't quite understand why there is. I have a non-nullable field in the database for a last_modified_by field which i didn't set when I created it and added it to the entityCollection, and I guess this would be causing it, but my question comes from why RIA doesn't call my Insert method in my service that I've created because I want to set that field there. Like so:
public void InsertPhoneNumber(PhoneNumbers pnum)
{
pnum.last_modified = DateTime.Today;
pnum.last_modified_by = Thread.CurrentPrincipal.Identity.Name;
if ((pnum.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(pnum, EntityState.Added);
}
else
{
this.ObjectContext.PhoneNumbers.AddObject(pnum);
}
}
But it's like RIA adds my object and calls it own Insert Method. So I rolled with it at first, and just set the property up in the UI, then it would give me this error:
Message = "Submit operation failed. An
error occurred while updating the
entries. See the inner exception for
details. Inner exception message:
Cannot insert explicit value for
identity column in table
'iset_trkr_writeback' when
IDENTITY_INSERT is set to OFF."
I never set the identity field to anything, I thought RIA would do this for me. But when i debug and take a look, it has a 0 for the value. But at least this time it calls my insert method in my service... Maybe I'm missing a big something for my process, but I really could use some help. Thanks:)
You using Entity Framework? If so, you need a [Key] attribute on at least one field in your metadata. Or create an identity/PK column (int/guid), and then update the metadata.