Entity Framework insert sublist into parent sublist - c#

I try to insert a list of Answers into Questions which in return it is inserted into Exams, all my code works just fine except one part, which is inserting new Answers.
Data is inserted just fine except Answers' data, whose data is not stored in the database, plus I try to get QuestionId so I can store it with Answer as a foreign key and I failed in that too.
API Controller
public IActionResult addExam([FromBody] Exams exam)
{
try
{
if (exam == null)
{
return StatusCode(401, "data is null");
}
var userId = this.help.GetCurrentUser(HttpContext);
Exams exams = new Exams
{
Name = exam.Name,
Number = exam.Number,
FullMarck = exam.FullMarck,
CreatedBy = userId,
CreatedOn = DateTime.Now,
Status = exam.Status
};
db.Exams.Add(exams);
var questionsList = new List<Questions>();
foreach (Questions item in exam.Questions)
{
var question = new Questions
{
ExamId = exam.Id,
Points = item.Points,
CreatedBy = userId,
CreatedOn = DateTime.Now,
Status = item.Status,
};
questionsList.Add(question);
}
exams.Questions = questionsList;
db.SaveChanges();
foreach (Questions item in exam.Questions)
{
var answersList = new List<Answers>();
foreach (Answers answers in item.Answers)
answersList.Add(new Answers
{
QuestionId = item.Id,
ExamAnswers = answers.ExamAnswers,
CreatedBy = userId,
CreatedOn = DateTime.Now
});
item.Answers = answersList;
}
db.SaveChanges();
return Ok("successfully created ");
}
catch (Exception e)
{
return StatusCode(500, e.InnerException.Message);
}
}

The way you build your graph is a bit unusual. I would have expected it more like a single set of nested loops that takes your supplied model and populates entity collections without forced ids. EF will track the IDs; you don't need to worry about them; when you add a new Answer to a particular question.Answers collection, you don't need to tell the Answer what it's QuestionID is; EF knows based on which question it was added to. If the ID for a question is not yet set, because it is generated by the db and no save has occurred, then saving the question will generate an ID and EF will ripple the change out to all the owned Answers in the question.Answers; you don't need to micro manage it
Here's a pseudo code of how I would expect it to go:
//model is an ExamModel
Exam e = new Exam(); //exam is a db entity
e.Title = model.ExamTitle; //model is not a db entity
foreach(QuestionModel mq in model.Questions){ //enumerate all the questionmodel we got from the front end and build a db entity graph
Question q = new Question(); //make new db entity
q.Subject = mq.QuestionHeader; //set entity property from model
q.Body = mq.BodyText; //set property from model
if(e.Questions == null) //not sure how your entities are declared, if this is already done elsewhere, remove it
e.Questions = new List<Question>();
e.Questions.Add(q); //add the question db entity to the exam db entity
//notice I didn't set the question id. EF will do that- it knows what exam this question belongs to
foreach(AnswerModel ma in mq.Answers){ //while our question db entity called q is still in scope let us add the related answers to it
Answer a = new Answer(); //create EF entity
a.Text = ma.AnswerText; //set db entity property from model property
if(q.Answers == null)
q.Answers = new List<Answer>();
q.Answers.Add(a); //add the answer to the question db entity
}
}
//exam e now has a collection of questions that have each a collection of answers, save it to the db
db.Exams.Add(e);
db.SaveChanges(); //only need one call to save changes on the whole graph
I think the way you've split your operations up hasn't created a connected graph of entities and/or the way you've forced the questionids of answers means EF hasn't kept the relationship to date upon saving.
You should also have a separation between the data objects arriving in your controller (I've called these ModelExam, ModelQuestion, ModelAnswer) and the entities in your EF (I've called these Exam, Question, Answer - yours are plural). This separation is achieved by having different classes for your front end controllers etc to use than your back end db context uses. At first it looks like things are being repeated for no good reason but eventually the system will become complex enough that not every db property can or should be exposed all the way to the front end and back, and the front end might need calculated or other non db based data. At this point you really need your front end data models to be completely separate things from your back end data entities

I think you need to call db.SaveChanges() after adding new exam. In your case exam id is not auto generated and it is always 0 , so you cant save question with examid 0;

item.Answers = answersList; is false
exam.Questions.Answers = answersList is true

Remove the first db.SaveChanges(); witch is before last foreach loop. This will insert all your data at once and should do the job.

Related

Adding a value in two different tables in my create controller [duplicate]

This question already has an answer here:
How to save the record mutipule table using Asp.net MVC Json
(1 answer)
Closed 3 years ago.
I have a Create controller that creates a record in one table [Kursus_kursist] using db.Add, dbSaveChanges().
I need to create a record in another table [jobcoach] based on one of the values (kursus_kursist_id which is an int), but I don't know how to pass the value.
public ActionResult Create([Bind(Include = "kursus_kursist_id,kursist_id,status, jc")] kursus_kursist kursus_kursist)
{
if (ModelState.IsValid)
{
db.kursus_kursist.Add(kursus_kursist);
db.SaveChanges();
db.jobcoach.Add(/*I THINK THE KURSUS_KURSIST_ID NEEDS TO GO HERE*/);
db.SaveChanges();
return RedirectToAction("../kursister/Index");
}
I've been able to create a record in both the usus_kursist and jobcoach tables, but in the jobcoach table I don't get the kursus_kursist_id - so basically an empty record (aside from the PK).
When you add to the database and save, that entity becomes tracked. The tracked entity have an assigned Id.
var myEntity = new SomeEntity(){
Name="Test",
CreatedDate = DateTime.UtcNow
}
dbContext.MyEntities.Add(myEntity);
dbContext.SaveChanges();
Console.WriteLine(myEntity.Id);
This will give you the proper Id EF assigned your entity.
If JobCoach is connected you could also add them in their existing relation.
var myEntity = new SomeEntity(){
Name="Test",
CreatedDate = DateTime.UtcNow,
MyChildObject = new ChildObject(){
Name="Something else",
CreatedDate = DateTime.UtcNow
}
dbContext.MyEntities.Add(myEntity);
dbContext.SaveChanges();
Console.WriteLine(myEntity.MyChildObject.Id);
So in your case you should be able to use your first kursist_thingie.Id after you saved that entity. Assign JobCoach.KursistId = kursist_thingie.Id or however you please.
Hope it helps!

How to update many to many in Entity Framework with AutoDetectChangesEnabled = false

Please, help me to handle this situation:
I meaningly switched off AutoDetectChangesEnabled and I load my
entities AsNoTracked() meaningly either.
And I can't update many-to-many relationship in this case:
Here is the code of Update method:
public void Update(User user)
{
var userRoleIds = user.Roles.Select(x => x.Id);
var updated = _users.Find(user.Id);
if (updated == null)
{
throw new InvalidOperationException("Can't update user that doesn't exists in database");
}
updated.Name = user.Name;
updated.LastName = user.LastName;
updated.Login = user.Login;
updated.Password = user.Password;
updated.State = user.State;
var newRoles = _roles.Where(r => userRoleIds.Contains(r.Id)).ToList();
updated.Roles.Clear();
foreach (var newRole in newRoles)
{
updated.Roles.Add(newRole);
}
_context.Entry(updated).State = EntityState.Modified;
}
All simple fields, like Name, LastName updated. But the set
of Roles for User doesn't get updated - it stays the same.
I tried loading Roles using
_context.Entry(updated).Collection("Roles").Load();
But I can't update this loaded set in any way.
I searched for similar items but failed to find the answer, thought it definitely already exists.
I'm really sorry for possible dublicate.
PS. I want to add that I don't want to delete or update child entities at all.
A lot of existing answers suggest manually delete / add child entities to database in whole, but it is not suitable for me.
Roles are independent entities, any other user can use them.
I just want to update User_Role table in database, but I can't.

How can I ensure that Entity Framework will create an association in a many-to-many relationship instead of a new record?

I have a problem with Entity Framework.... I don't know it well.
I am working on a seemingly simple task of associating an existing contact to a new order in a system I'm working on. Contacts will always exist prior to being associated to the order.
Simplified database tables...
Order
OrderID
Status
Created
Contact
ContactID
FirstName
LastName
ContactToOrderMap
OrderID
ContactID
I've linked these two tables in the DbContext.
modelBuilder.Entity<Order>()
.HasMany<Contact>(a => a.Contacts)
.WithMany()
.Map(a =>
{
a.ToTable("ContactToOrderMap");
a.MapLeftKey("OrderID");
a.MapRightKey("ContactID");
});
I wrote a unit test that simply creates an order, a contact, adds the contact to a list member on the order and attempts to save.
// setup
var order = new Order() { // initialize required fields };
// add a contact that already exists in the database
var orderContact = new Contact() { ID = 2 };
order.Contacts.Add(orderContact);
// call
logic.Save(ref order);
Eventually this order is saved after some unrelated business logic is performed...
// Add to collection
_db.Orders.Add(obj);
// Commit changes
_db.SaveChanges();
>> Here I receive errors related to required database fields on the Contact table. Not what I'm looking for as I really just want to add an association record in the ContactToOrderMap table.
As a next step, I tried to retrieve the existing contacts from the database prior to saving the order. This code's not super clean, but it should explain what I'm attempting.
if (currentOrder.Contacts != null)
{
var matchedContacts = new List<Contact>();
foreach (var con in currentOrder.Contacts)
{
matchedContacts.Add(_contactLogic.Get(con.ID));
}
currentOrder.Contacts.Clear();
foreach (var item in matchedContacts)
{
currentOrder.Contacts.Add(item);
}
}
This created the association, but created a new Contact (new unique ID) as well.
The goal is to create an association and a new order only, not create a new contact.
Any ideas or pointers to get me going in the right direction?
Please let me know if additional information is required.
Edit: Fixed code - renamed obj to currentOrder.
After the line
var orderContact = new Contact() { ID = 2 };
do
_db.Contacts.Attach(orderContact);
Now the contact is part of the context in an Unchanged state, and
order.Contacts.Add(orderContact);
will not change its state to Added anymore.

dbset.local updating database: a duplicate value cannot be inserted into a unique index

I am getting an error when calling entities.savechanges() on my EF 4.3.1. My database is a sql ce v4 store and I am coding in the mvvm pattern. I have a local version of my context that I send to an observable collection and modify etc. This works fine, and when I call savechanges() when no rows exist in the database the objects persist fine. When I reload the application, the objects are populated in my listbox as they should, however if I add another object and call savechanges() I get an error saying that a duplicate value cannot be inserted into a unique index.
From my understanding it means that the context is trying to save my entities to the datastore, but it seems to be adding my untouched original objects as well as the new one. I thought it would leave them alone, since their state is unchanged.
private void Load()
{
entities.Properties.Include("Images").Load();
PropertyList = new ObservableCollection<Property>();
PropertyList = entities.Properties.Local;
//Sort the list (based on previous session stored in database)
var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
PropertyList.Clear();
sortList.ForEach(PropertyList.Add);
propertyView = CollectionViewSource.GetDefaultView(PropertyList);
if (propertyView != null) propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);
private void NewProperty()
{
try
{
if (PropertyList != null)
{
Property p = new Property()
{
ID = Guid.NewGuid(),
AgentName = "Firstname Lastname",
Address = "00 Blank Street",
AuctioneerName = "Firstname Lastname",
SaleTitle = "Insert a sales title",
Price = 0,
NextBid = 0,
CurrentImage = null,
Status = "Auction Pending",
QuadVis = false,
StatVis = false, //Pause button visibility
Sort = PropertyList.Count + 1,
};
PropertyList.Add(p);
SaveProperties();
}
private void SaveProperties()
{
try
{
foreach (var image in entities.Images.Local.ToList())
{
if (image.Property == null)
entities.Images.Remove(image);
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
entities.SaveChanges();
}
Without commenting on all the code here this is the bit that's causing the specific problem you bring up:
//Sort the list (based on previous session stored in database)
var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
PropertyList.Clear();
sortList.ForEach(PropertyList.Add);
This code:
Starts with entities that have been queried and are being tracked by the context as Unchanged entities. That is, entities that are known to already exist in the database.
Creates a new sorted list of these entities.
Calls Clear on the local collection causing each tracked entity to be marked as deleted and removed from the collection.
Adds each entity back to the context putting it now in an Added state meaning that it is new and will be saved to the database when SaveChanges is called,
So effectively you have told EF that all the entities that exist in the database actually don't exist and need to be saved. So it tries to do this and it results in the exception you see.
To fix this don't clear the DbContext local collection and add entities back. Instead you should sort in the view using the local collection to back the view.
It sounds like you're adding the existing entities to the context (which marks them for insertion) instead of attaching them (which marks them as existing, unmodified).
I'm also not sure that new Guid() isn't returning the same guid... I always use Guid.NewGuid() http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx

LINQ to SQL Insert Multiple Tables Question

I have 3 tables. A primary EmploymentPlan table with PK GUID EmploymentPlanID and 2 FK's GUID PrevocServicesID & GUID JobDevelopmentServicesID. There are of course other fields, almost exclusively varchar(). Then the 2 secondary tables with the corresponding PK to the primary's FK's.
I am trying to write the LINQ INSERT Method and am struggling with the creation of the keys. Say I have a method like below. Is that correct? Will that even work? Should I have seperate methods for each?
Also, when inserting I didn't think I needed to provide the PK for a table. It is auto-generated, no?
Thanks.
public static void InsertEmploymentPlan(int planID, Guid employmentQuestionnaireID, string user, bool communityJob, bool jobDevelopmentServices, bool prevocServices, bool transitionedPrevocIntegrated, bool empServiceMatchPref)
{
using (var context = MatrixDataContext.Create())
{
var empPrevocID = Guid.NewGuid();
var prevocPlan = new tblEmploymentPrevocService
{
EmploymentPrevocID = empPrevocID
};
context.tblEmploymentPrevocServices.InsertOnSubmit(prevocPlan);
var empJobDevID = Guid.NewGuid();
var jobDevPlan = new tblEmploymentJobDevelopmetService()
{
JobDevelopmentServicesID = empJobDevID
};
context.tblEmploymentJobDevelopmetServices.InsertOnSubmit(jobDevPlan);
var empPlan = new tblEmploymentQuestionnaire
{
CommunityJob = communityJob,
EmploymentQuestionnaireID = Guid.NewGuid(),
InsertDate = DateTime.Now,
InsertUser = user,
JobDevelopmentServices = jobDevelopmentServices,
JobDevelopmentServicesID =empJobDevID,
PrevocServices = prevocServices,
PrevocServicesID =empPrevocID,
TransitionedPrevocToIntegrated =transitionedPrevocIntegrated,
EmploymentServiceMatchPref = empServiceMatchPref
};
context.tblEmploymentQuestionnaires.InsertOnSubmit(empPlan);
context.SubmitChanges();
}
}
I understand I can use more then 1 InsertOnSubmit(), See this question, I just don't understand how that would apply to my situation and the PK/FK creation.
The pk can be auto generated when the table's definition in the db does it for you. Also the property for the corresponding pk on the linq model has to configured to be updated after the insert, so it gets the auto generated ID.
I don't think the relation on those tables is on your linq model. Otherwise you should be able to do:
using (var context = MatrixDataContext.Create())
{
var empPlan = new tblEmploymentQuestionnaire
{
CommunityJob = communityJob,
InsertDate = DateTime.Now,
InsertUser = user,
JobDevelopmentServices = jobDevelopmentServices,
JobDevelopmentService = new tblEmploymentJobDevelopmetService(),
PrevocServices = prevocServices,
PrevocService = new tblEmploymentPrevocService(),
PrevocServicesID =empPrevocID,
TransitionedPrevocToIntegrated =transitionedPrevocIntegrated,
EmploymentServiceMatchPref = empServiceMatchPref
};
context.tblEmploymentQuestionnaires.InsertOnSubmit(empPlan);
context.SubmitChanges();
}
ps. not having the relation in the model is a design decision, so the above doesn't mean that's the only way to do it. The way you showed (with the extra SubmitChanges calls as in the other answer) is perfectly valid, just responds to a different design.
I think the issue is (if I understand it correctly) you are deferring the inserting, except you don't know it...
Since you're creating FKs but differing their insertion until the end, it doesn't know what to do, so when you try to create the main entry it's enforcing the FK constraints (which might not exist yet), thus failing. Try creating the FK entries and actually submitting the changes to the database before insert the main entry.
For example, say you have the following tables:
Child
Toy
ToyOwner
ToyOwner has FK constraints on Child and Toy. If the entries are missing in that table, you will not be able to insert an entry into ToyOwner. So you'd have to do something like the following:
Child myChild;
Toy myToy;
//Queue up the changes that are going to be submitted
InsertOnSubmit(myChild)
InsertOnSubmit(myToy)
//Submit the queue
SubmitChanges();
//Now that those FKs are filled, we can insert the main entry with those FK values
ToyOwner = new myToyOwner
myToyOwner.Child = myChild
myToyOwner.Toy = myToy
//And insert the new queue into the DB
InsertOnSubmit(myToyOwner)
SubmitChanges();

Categories

Resources