EF 4 insert entity in relationship with existing entity - c#

using (EntitiesContainer db = new EntitiesContainer())
{
Language language = db.Languages.SingleOrDefault(x => x.Culture == _session.Language);
Language language2 = new Language { Id = action.Language.Id };
Operation operation = new Operation { Id = action.Operation.Id };
//Operation operation = db.Operations.SingleOrDefault(x => x.Id == action.Operation.Id);
if (!language.Id.Equals(language2.Id))
{
db.Languages.Attach(language2);
action.Language = language2;
}
else
{
action.Language = language;
}
db.Operations.Attach(operation);
//db.ObjectStateManager.ChangeObjectState(operation, System.Data.EntityState.Unchanged);
action.Operation = operation;
//operation.Internals.Add(action);
action.CurrentDetail.Language = language;
action.CurrentDetail.Id = Guid.NewGuid();
action.Id = Guid.NewGuid();
db.SaveChanges();
}
Hello I Try all this scenario in commentary, for link my existing operation to the internal action that inherited from action .. but in any of this scenario, he throw me an error like he want to insert a new operation in the DB (dbo.Operations can accept "Action" NULL value) but the Entity already exist.. Can someone please, give me the golden rule .. to insert entity with relation .. existing or not in EF. It's driving me crazy!
Cordialy,
Julien.

If i understand you correctly (your trying to update a relationship), you can use the stub technique:
Language lang = new Language { Id = action.language_id }; // create stub with ID
db.Languages.Attach(lang); // attach stub to graph
action.language = lang; // update relationship
db.SaveChanges();

Related

Update Object without Select EF6 MySQL

Is it possible to update objects with Entity Framework, without grabbing them first?
Example: Here, I have a function that provides a Primary Key to locate the objects, pulls them, then updates them. I would like to eliminate having to pull the objects first, and simply run an UPDATE query. Removing the need for the SELECT query being generated.
public async Task<int> UpdateChecks(long? acctId, string payorname, string checkaccountnumber, string checkroutingnumber, string checkaccounttype)
{
using (var max = new Max(_max.ConnectionString))
{
var payments = await
max.payments.Where(
w =>
w.maindatabaseid == acctId && (w.paymentstatus == "PENDING" || w.paymentstatus == "HOLD")).ToListAsync();
payments.AsParallel().ForAll(payment =>
{
payment.payorname = payorname;
payment.checkaccountnumber = checkaccountnumber;
payment.checkroutingnumber = checkroutingnumber;
payment.checkaccounttype = checkaccounttype;
payment.paymentmethod = "CHECK";
payment.paymentstatus = "HOLD";
});
await max.SaveChangesAsync();
return payments.Count;
}
}
You can use the Attach() command to attach an entity you already know exists and then call SaveChanges() will will call the appropriate update method. Here is some sample code from the MSDN article on the topic:
on the subject:
var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" };
using (var context = new BloggingContext())
{
context.Entry(existingBlog).State = EntityState.Unchanged;
// Do some more work...
context.SaveChanges();
}
Note that this is general EF logic, not related to any specific database implementation.

Using one of DBML schemes depending on parameter

I have an application which should support two versions of databases based on configuration parameter.
The problem is that there is some slight differences between the types in tables of those databases.
For example on old database, the field is of type tinyint and now it's smallint.
When I try to use the new .dbml file I get a cute InvalidCastException when trying to select anything.
I need two different .dbml schemes with same tables and column names but different types.
I'd like to avoid duplicating the code like:
if (newDatabaseVersionFlag)
{
Data.DatabaseDataContext context = new Data.DatabaseDataContext(connectionString);
Data.Something item = context.Somethings.SingleOrDefault(e => e.Id == id);
item.Sth = (short)5;
item.Sth2 = "sample code";
}
else
{
Data2.DatabaseDataContext context2 = new Data2.DatabaseDataContext(connectionString);
Data2.Something item2 = context2.Somethings.SingleOrDefault(e => e.Id == id);
item2.Sth = (byte)5;
item2.Sth2 = "sample code";
}
And there's a lot more of code than that...
What is the best way to make it neat and clean?
You will be best to create a Proxy Data Context.
The issue is if you have different data types, your compiler will fail unless you are using the var
public class UseMeDataContext:System.Data.Linq.DataContext
{
...
}
UseMeDataContext db ;
if (newDatabaseVersionFlag)
{
db = ((System.Data.Linq.DataContext) new Data.DatabaseContext(connectionString));
}
else
{
db = ((System.Data.Linq.DataContext) new Data2.DatabaseDataContext(connectionString));
}
db.Something item2 = UseMeContext.Somethings.SingleOrDefault(e => e.Id == id);
item2.Sth = (TypeOf(item2.sth))5; // May or May not Need typing
item2.Sth2 = "sample code";

Can Entity Framework add many related entities with single SaveChanges()?

I am writing many (20+) parent child datasets to the database, and EF is requiring me to savechanges between each set, without which it complains about not being able to figure out the primary key. Can the data be flushed to the SQL Server so that EF can get the primary keys back from the identities, with the SaveChanges being sent at the end of writing all of the changes?
foreach (var itemCount in itemCounts)
{
var addItemTracking = new ItemTracking
{
availabilityStatusID = availabilityStatusId,
itemBatchId = itemCount.ItemBatchId,
locationID = locationId,
serialNumber = serialNumber,
trackingQuantityOnHand = itemCount.CycleQuantity
};
_context.ItemTrackings.Add(addItemTracking);
_context.SaveChanges();
var addInventoryTransaction = new InventoryTransaction
{
activityHistoryID = newInventoryTransaction.activityHistoryID,
itemTrackingID = addItemTracking.ItemTrackingID,
personID = newInventoryTransaction.personID,
usageTransactionTypeId = newInventoryTransaction.usageTransactionTypeId,
transactionDate = newInventoryTransaction.transactionDate,
usageQuantity = usageMultiplier * itemCount.CycleQuantity
};
_context.InventoryTransactions.Add(addInventoryTransaction);
_context.SaveChanges();
}
I would like to do my SaveChanges just once at the end of the big loop.
You don`t need to save changes every time if you use objects refernces to newly created objects not IDs:
var addItemTracking = new ItemTracking
{
...
}
_context.ItemTrackings.Add(addItemTracking);
var addInventoryTransaction = new InventoryTransaction
{
itemTracking = addItemTracking,
...
};
_context.InventoryTransactions.Add(addInventoryTransaction);
...
_context.SaveChanges();
Since they're all new items rather than
itemTrackingID = addItemTracking.ItemTrackingID,
you could go with
addItemTracking.InventoryTransaction = addInventoryTransaction;
(or whatever the associated navigation property is) and pull the _context.SaveChanges() out of the loop entirely. Entity Framework is very good at inserting object graphs when everything is new. When saving object graphs containing both new and existing items setting the associated id is always safer.
How about:
var trackingItems = itemCounts
.Select(i => new ItemTracking
{
availabilityStatusID = availabilityStatusId,
itemBatchId = i.ItemBatchId,
locationID = locationId,
serialNumber = serialNumber,
trackingQuantityOnHand = i.CycleQuantity
});
_context.ItemTrackings.AddRange(trackingItems);
_context.SaveChanges();
var inventoryTransactions = trackingItems
.Select(t => new InventoryTransaction
{
activityHistoryID = newInventoryTransaction.activityHistoryID,
itemTrackingID = t.ItemTrackingID,
personID = newInventoryTransaction.personID,
usageTransactionTypeId = newInventoryTransaction.usageTransactionTypeId,
transactionDate = newInventoryTransaction.transactionDate,
usageQuantity = usageMultiplier * t.trackingQuantityOnHand
});
_context.InventoryTransactions.AddRange(inventoryTransactions);
_context.SaveChanges();
However I haven't worked with EF for quite a while and above code is written in notepad so I cannot vouch for it

Why is a validation exception thrown on a field which is not being updated?

I am using EF 6.0.2 and trying to update only the Status field of an entity.
var drama = new Drama { Id = id };
using (var ctx = new DataContext()) {
ctx.Dramas.Attach(drama);
drama.Status = state;
ctx.SaveChanges();
}
This throws a ValidationException: The ClassName field is required.
The entity already exists, and is valid (including having a ClassName). Id is the entity key.
What's going on here to cause the exception to be thrown?
You're saying there already is an entity with the given Id in the database and you'd only like to change its Status value?
That's not what you're doing in your code. You have created a new entity with the given Id and set only its Status value. That's why it's throwing a ValidationException: ClassName field is empty unless you're setting it in Drama constructor.
To modify an existing entity you should first retrieve it from the database, then modify it and save the changes:
using (var ctx = new DataContext()) {
var drama = ctx.Dramas.Single(d => d.Id == id);
drama.Status = state;
ctx.SaveChanges();
}
EDIT:
If you want to simulate a scenario of editing a detached entity, you still need its values to be valid. Try fulfilling all the validation requirements before attaching the entity (i.e. fill in the ClassName), then Attach it, update Status and SaveChanges:
var drama = new Drama { Id = id, ClassName = "Dummy" };
using (var ctx = new DataContext()) {
ctx.Dramas.Attach(drama);
drama.Status = state;
ctx.SaveChanges();
}
When you do edits in EF, you should get the entire record that you are editing and then put the fields whatever you want to update.
using (var ctx = new DataContext()) {
drama = ctx.Dramas.Where(O => O.Id = id);
drama.Status = state;
ctx.Dramas.Attach(drama);
ctx.SaveChanges();
}
Hope this helps

C# Linq is removing a value from my entity

So, in a desperate attempt to wrangle EntityFramework into being usable. I am here..
private MyEntity Update(MyEntity orig)
{
//need a fresh copy so we can attach without adding timestamps
//to every table....
MyEntity ent;
using (var db = new DataContext())
{
ent = db.MyEntities.Single(x => x.Id == orig.Id);
}
//fill a new one with the values of the one we want to save
var cpy = new Payment()
{
//pk
ID = orig.ID,
//foerign key
MethodId = orig.MethodId,
//other fields
Information = orig.Information,
Amount = orig.Amount,
Approved = orig.Approved,
AwardedPoints = orig.AwardedPoints,
DateReceived = orig.DateReceived
};
//attach it
_ctx.MyEntities.Attach(cpy, ent);
//submit the changes
_ctx.SubmitChanges();
}
_ctx is an instance variable for the repository this method is in.
The problem is that when I call SubmitChanges, the value of MethodId in the newly attached copy is sent to the server as 0, when it is in fact not zero if I print it out after the attach but before the submit. I am almost certain that is related to the fact that the field is a foreign key, but I still do not see why Linq would arbitrarily set it to zero when it has a valid value that meets the requirements of the constraint on the foreign key.
What am I missing here?
You should probably set Method = orig.Method, but I can't see your dbml, of course.
I think you need to attach the foreign key reference
var cpy = new Payment()
{
//pk
ID = orig.ID,
//other fields
Information = orig.Information,
Amount = orig.Amount,
Approved = orig.Approved,
AwardedPoints = orig.AwardedPoints,
DateReceived = orig.DateReceived
};
//create stub entity for the Method and Add it.
var method = new Method{MethodId=orig.MethodId)
_ctx.AttachTo("Methods", method);
cpy.Methods.Add(method);
//attach it
_ctx.MyEntities.Attach(cpy, o);
//submit the changes
_ctx.SubmitChanges();

Categories