How to Clone POCO entity and add to context - c#

I am using EF4 and I have create POCO objects with proxies from my database structure . I have a POCO (object) which has lots of relationships to other entities.
I created a deep copy of the object using DataContractSerializer and BinaryFormatter and lets call it clonedObject.
function used for cloning is:
public T CloneProxy<T>(T source)
{
var dcs = new System.Runtime.Serialization
.DataContractSerializer(typeof(T));
string filePath = "Initiative.txt";
using (FileStream file = new FileStream(filePath, FileMode.Create))
{
(new BinaryFormatter()).Serialize(file, source);
}
context.CreateProxyTypes(new Type[] { typeof(Initiative) });
using (FileStream file = new FileStream(filePath, FileMode.Open))
{
return (T)(new BinaryFormatter()).Deserialize(file);
}
}
Now that I have clonedObject, how do I add it to the context? how do I add it to the database?
my object (just giving you an Idea of the POCO Initiative):
Initiative
{
InitI
InitName
<collection>Comments
}
Comments
{
CommentI
<FK>InitI
}
Here are some of the way I have done and errors I have received.
cloneInit.InitI = 0;
Data_Business.RQRMComment[] arr = new Data_Business.RQRMComment[1];
arr = cloneInit.RQRMComments.ToArray();
for (int x = 0; x < arr.Length; x++) //each (var x in cloneInit.RQRMComments)
{
RQRMComment thisC = arr[x];
int y = thisC.InitI;
thisC.InitI = 0;
thisC.ID = 0;
}
Context.AddObject("Initiatives", cloneInit);
Context.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
Error:
ex = {"The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object."}
Please help, I have spent too much time on this. Thank you.

I have had a need to clone my Entities for the purpose of re-displaying the data on a form so that a user can choose to "Create & Add Similar" in an effort to reduce the amount of effort a user needs to expend in order to add a range of similar items to my DB.
I checked out a few options including reflection & serialization but they are messy for what I am trying to achieve, I then discovered that I can overcome the "XYZ is part of the object's key information and cannot be modified" issue - i.e. set my entities primary key to 0 after insert (Save Changes) - with the following code:
MyDbEntities bb = new MyDbEntities();
//Add & Save new entry
db.Product.AddObject(product);
db.SaveChanges();
//Reset entity
db.ObjectStateManager.ChangeObjectState(product, System.Data.EntityState.Added);
product.ProductId = 0;

Related

How to add a partial entry into relational DB with entity core

The Program:
My program reads from a file and then stores the values in a relational db.
The Models
ClientDetails, ClientVehicle, ClientOrder
The Relationship:
1 client can have many vehicles
1 vehicle can have many orders
The Problem:
The program does not support duplicate data.
The Struggle
I need to be able to not add a client if the client already exists but still have the client linked to the vehicle when storing the vehicle (Entity core handles the foreign keys for me, but when simply skipping half the record I get foreign key constraints). This same process needs to work if a vehicle with the same registration plate gets added.
The Struggle Continues
To top off this problem I also need to be able to do these checks on the file itself as I only save the context changes into the db at the end of the whole process so if the file contains 2 records with the same user but different vehicles (1 client can have many vehicles) then I need to only add the user once.
The Hope
I am hoping that somebody can tell me if there is a built in entity core method that can sort this out for me. I have seen some entity framework things that I estimate would fix this but I can't find the entity core equivalent. for example _context.AddIfNotExists. Or something along the lines of _context.AddOrUpdate(entity, k = k.id = record[0]). If a post like this already exist please direct me as I could not find it myself.
NOTE
When a duplicate client is found the rest of the entry should still happen as this is an "order", its only the client that needs to be skipped (or vehicle in the event of the same registration plate).
The Code Snippet In Question
public void LoadCsv(string path)
{
var lines = File.ReadAllLines(path).Where(x => !string.IsNullOrWhiteSpace(x));
_now = DateTime.Now;
var count = 0;
foreach (var line in lines)
{
if (count > 0)
{
var record = line.Split('|').ToList();
var entityClientDetails = AddClientDetails(record, _now);
var entityClientVehicle = AddClientVehicle(record, _now);
var entityClientOrder = AddClientOrder(record, _now);
entityClientDetails.ClientVehicles.Add(entityClientVehicle);
entityClientVehicle.ClientOrders.Add(entityClientOrder);
_context.Add(entityClientDetails);
}
count++;
}
_context.SaveChanges();
}
Try this
public void LoadCsv(string path)
{
var clients = _context.ClientDetails.AsQueryable();
var lines = File.ReadAllLines(path).Where(x => !string.IsNullOrWhiteSpace(x));
_now = DateTime.Now;
var count = 0;
foreach (var line in lines)
{
if (count > 0)
{
var record = line.Split('|').ToList();
var entityClientDetails = AddClientDetails(record, _now);
var entityClientVehicle = AddClientVehicle(record, _now);
var entityClientOrder = AddClientOrder(record, _now);
var clientExist = clients.Where(x => x.RegistrationPlate == entityClientDetails.RegistrationPlate).FirstOrDefault();
entityClientVehicle.ClientOrders.Add(entityClientOrder);
if (clientExist != null)
{
entityClientVehicle.ClientDetailID = clientExist.Id;
_context.Add(entityClientVehicle);
}
else
{
entityClientDetails.ClientVehicles.Add(entityClientVehicle);
_context.Add(entityClientDetails);
}
}
count++;
}
_context.SaveChanges();
}

Attaching objects retrieved from multiple sources

I have three classes, Fish (which contains two properties of type Chips and MushyPeas respectively), MushyPeas (which contains a property of type Chips) and Chips (which has a Name property).
I am running the following piece of hypothetical code:
int chipsId;
using (var db = new FishContext())
{
var creationChips = new Chips() { Name = "A portion of chips" };
db.Chips.Add(creationChips);
db.SaveChanges();
chipsId = creationChips.ChipsId;
}
Chips retrievedChips1;
using (var db = new FishContext())
{
retrievedChips1 = db.Chips.Where(x => x.ChipsId == chipsId).ToList()[0];
}
Chips retrievedChips2;
using (var db = new FishContext())
{
retrievedChips2 = db.Chips.Where(x => x.ChipsId == chipsId).ToList()[0];
}
using (var db = new FishContext())
{
db.Chips.Attach(retrievedChips1);
db.Chips.Attach(retrievedChips2);
var mushyPeas = new MushyPeas() { Chips = retrievedChips2 };
var fish = new Fish() { Chips = retrievedChips1, MushyPeas = mushyPeas };
db.Fish.Add(fish);
db.ChangeTracker.DetectChanges();
db.SaveChanges();
}
This is to simulate a situation in my real app, in which EF objects (which may actually represent the same database record) are loaded from a variety of different DbContexts and then added to an object tree in another DbContext.
If I don't call the two db.Chips.Attach lines, then brand new Chips entities are created when the Fish object is saved to the database, and assigned new IDs.
Calling db.Chips.Attach solves this issue for one of the retrieved obejcts, but the second Attach call fails with the exception "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."
What is the best way to achieve what I want to achieve here?
As a grizzled EF vet, I've come to the conclusion that it's best to avoid using Attach in many cases.
The exception "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key" is usually misleading since the object that you're trying to attach isn't actually attached to the data context. What happens when you attach an object is it recursively attaches any entities that it references. So, if you attach an entity to the data context, and then attach another entity that references any entity that was implicitly attached previously, you will get this error. The solution is pretty simple:
using (var db = new FishContext())
{
var chips1 = db.Chips.Find(retrievedChips1.Id);
var chips2 = db.Chips.Find(retrievedChips2.Id);
var mushyPeas = new MushyPeas() { Chips = chips2 };
var fish = new Fish() { Chips = chips1, MushyPeas = mushyPeas };
db.Fish.Add(fish);
db.ChangeTracker.DetectChanges();
db.SaveChanges();
}
This guarantees that both entities will be attached to the data context without any sort of ObjectStateManager issues.
You could query the Local collection to check if an entity with the same key is already attached and if yes, use the attached entity:
using (var db = new FishContext())
{
var attachedChips1 = db.Chips.Local
.SingleOrDefault(c => c.ChipsId == retrievedChips1.ChipsId);
if (attachedChips1 == null)
{
db.Chips.Attach(retrievedChips1);
attachedChips1 = retrievedChips1;
}
var attachedChips2 = db.Chips.Local
.SingleOrDefault(c => c.ChipsId == retrievedChips2.ChipsId);
if (attachedChips2 == null)
{
db.Chips.Attach(retrievedChips2);
attachedChips2 = retrievedChips2;
}
var mushyPeas = new MushyPeas() { Chips = attachedChips2 };
var fish = new Fish() { Chips = attachedChips1, MushyPeas = mushyPeas };
//...
}
(The first check doesn't make sense in this simple example because a new context is empty with nothing attached to it. But you get the idea...)
However, in the case that you also want to update the related entities (for example by setting the state to Modified after attaching) it would be a problem if retrievedChips1 and retrievedChips2 have (except the key value) different property values. You had to decide somehow which is the "correct one". But that would be business logic. You just have to hand over one of them to EF and only one. In your scenario it wouldn't matter which one you use because you are only creating a relationship and for this EF will only care about the key value.
Side note: Instead of ...ToList()[0] the more natural way would be ...First() (or Single() in this case because you are querying the key).

bulk insert with linq-to-sql

I have a query that looks like this:
using (MyDC TheDC = new MyDC())
{
foreach (MyObject TheObject in TheListOfMyObjects)
{
DBTable TheTable = new DBTable();
TheTable.Prop1 = TheObject.Prop1;
.....
TheDC.DBTables.InsertOnSubmit(TheTable);
}
TheDC.SubmitChanges();
}
This query basically inserts a list into the database using linq-to-sql. Now I've read online that L2S does NOT support bulk operations.
Does my query work by inserting each element at a time or all of them in one write?
Thanks for the clarification.
I modified the code from the following link to be more efficient and used it in my application. It is quite convenient because you can just put it in a partial class on top of your current autogenerated class. Instead of InsertOnSubmit add entities to a list, and instead of SubmitChanges call YourDataContext.BulkInsertAll(list).
http://www.codeproject.com/Tips/297582/Using-bulk-insert-with-your-linq-to-sql-datacontex
partial void OnCreated()
{
CommandTimeout = 5 * 60;
}
public void BulkInsertAll<T>(IEnumerable<T> entities)
{
using( var conn = new SqlConnection(Connection.ConnectionString))
{
conn.Open();
Type t = typeof(T);
var tableAttribute = (TableAttribute)t.GetCustomAttributes(
typeof(TableAttribute), false).Single();
var bulkCopy = new SqlBulkCopy(conn)
{
DestinationTableName = tableAttribute.Name
};
var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
var table = new DataTable();
foreach (var property in properties)
{
Type propertyType = property.PropertyType;
if (propertyType.IsGenericType &&
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = Nullable.GetUnderlyingType(propertyType);
}
table.Columns.Add(new DataColumn(property.Name, propertyType));
}
foreach (var entity in entities)
{
table.Rows.Add(
properties.Select(
property => property.GetValue(entity, null) ?? DBNull.Value
).ToArray());
}
bulkCopy.WriteToServer(table);
}
}
private bool EventTypeFilter(System.Reflection.PropertyInfo p)
{
var attribute = Attribute.GetCustomAttribute(p,
typeof(AssociationAttribute)) as AssociationAttribute;
if (attribute == null) return true;
if (attribute.IsForeignKey == false) return true;
return false;
}
The term Bulk Insert usually refers to the SQL Server specific ultra fast bcp based SqlBulkCopy implementation. It is built on top of IRowsetFastLoad.
Linq-2-SQL does not implement insert using this mechanism, under any conditions.
If you need to bulk load data into SQL Server and need it to be fast, I would recommend hand coding using SqlBulkCopy.
Linq-2-SQL will attempt to perform some optimisations to speed up multiple inserts, however it still will fall short of many micro ORMs (even though no micro ORMs I know of implement SqlBulkCopy)
It will generate a single insert statement for every record, but will send them all to the server in a single batch and run in a single transaction.
That is what the SubmitChanges() outside the loop does.
If you moved it inside, then every iteration through the loop would go off to the server for the INSERT and run in it's own transaction.
I don't believe there is any way to fire off a SQL BULK INSERT.
LINQ Single Insert from List:
int i = 0;
foreach (IPAPM_SRVC_NTTN_NODE_MAP item in ipapmList)
{
++i;
if (i % 50 == 0)
{
ipdb.Dispose();
ipdb = null;
ipdb = new IPDB();
// .NET CORE
//ipdb.ChangeTracker.AutoDetectChangesEnabled = false;
ipdb.Configuration.AutoDetectChangesEnabled = false;
}
ipdb.IPAPM_SRVC_NTTN_NODE_MAP.Add(item);
ipdb.SaveChanges();
}
I would suggest you take a look at N.EntityFramework.Extension. It is a basic bulk extension framework for EF 6 that is available on Nuget and the source code is available on Github under MIT license.
Install-Package N.EntityFramework.Extensions
https://www.nuget.org/packages/N.EntityFramework.Extensions
Once you install it you can simply use BulkInsert() method directly on the DbContext instance. It support BulkDelete, BulkInsert, BulkMerge and more.
BulkInsert()
var dbcontext = new MyDbContext();
var orders = new List<Order>();
for(int i=0; i<10000; i++)
{
orders.Add(new Order { OrderDate = DateTime.UtcNow, TotalPrice = 2.99 });
}
dbcontext.BulkInsert(orders);

Unable to Save List<dynamic> using Rob Conery's Massive

I'm using Rob Conery's Massive to connect to my database, but I don't seem to be able to be able to save a list of dynamic objects to the database. I thought this was supported though.
Here's the code I am attempting to use:
int numberOfChildren = int.Parse(Request.Form["numberOfChildren"]);
List<dynamic> children = new List<dynamic>();
for(int i = 1; i <= numberOfChildren; i++) {
dynamic child = new ExpandoObject();
child.FamilyID = familyId;
child.Type = "CHILD";
child.LastName = Request.Form[i + "-childLastName"];
child.FirstName = Request.Form[i + "-childFirstName"];
child.SendSmsAlerts = false;
child.Gender = Request.Form[i + "-childGender"];
child.Birthdate = Request.Form[i + "-childBirthdate"];
children.Add(child);
}
var people = new People();
people.Save(children);
I get a "Parameter count mismatch." error on line 78 of Massive.cs
Everything works fine if i only pass in a single dynamic object at a time, the error is only raised when I attempt to pass in the list. Based on the documentation on GitHub I thought this was supported and it would save all the children in one transaction.
Save takes an params array not a list.
people.Save(children.ToArray());

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