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!
Related
So I'm trying to update ONLY specific data in my MVC application. Whenever I only edit a list view, I would edit the data (which works) but the rest of the database table would be NULL.
So for example -
Fields in List View
EmployeeID, FirsName, LastName, Email, Moderator, Admin
Fields not in list view
LoginID
So in my edit page, I have set up read-only for the info like EmployeeID, FirstName, LastName, and Email with two checkboxes for Moderator and Admin which are the only values I want to edit.
They work fine but when doing so, the data in the database for LoginID only becomes NULL.
Here is the code from my edit GET and POST methods
GET
public ActionResult Edit(int id)
{
EmpContext ec = new AppContext();
Employee e = ec.Employees.Single(x => x.Id == id);
return View();
}
POST
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
EmpContext ec = new EmpContext();
ec.Entry(employee).State = EntityState.Modified;
ec.SaveChanges();
return RedirectToAction("List");
}
return View(employee);
}
So, how would I stop this executing in the field LoginID within the database when there are no text boxes at all for LoginID, even on the edit page?
According to Attaching an existing but modified entity to the context
When you change the state to Modified all the properties of the entity
will be marked as modified and all the property values will be sent to
the database when SaveChanges is called.
Thus, this Entry method will update all the properties. For my recommendation, to update some Entity's columns only, you have to manually query the record and update desired properties only.
In your Edit (POST) method, change the Entry() way to:
EmpContext ec = new EmpContext();
Employee _employee = ec.Employees.Single(x => x.Id == employee.Id);
// TO-DO Update required properties only
_employee.FirstName = employee.FirstName;
ec.SaveChanges();
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.
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.
I have a create User ASP.NET MVC 3 page - on the page there is a multiselect box of cars which is prepoulated from a Cars table in my DB. On save User I return a list of ints of CarIds that the user is associated with. I am using WCF services and Entity Framework 5.0 to get my data back to SQL Server and save to SQL server.
I then have a Table in my DB CarUser - it just contain a user Id and a client id - two FK's to the UserId in User Table and the CarId in the Cars table.
When I create a User it is updating the CarUser table is getting updated correctly (so for e.g - it might look like this)
CarId UserId
2 4
3 4
4 4
5 4
So it is looking correct in that my CarUser table is showing User 4 associated with the 4 CarIds that were seleted from the Multi Select box. However the problem I am having is that it is re-creating the Cars in the cars table - so it is creating Car Id 9,10,11,12 for example but the details of them are exactly the same as 2,3,4,5
The code I have wrote for this is below:
public User User_Create(User user, List<int> carIds)
{
DAL.CarUserWCFFServiceImpl carUser = new DAL.CarUserWCFFServiceImpl();
// cal the DAL layer to do the create
User newUser = carUser.User_Create(user);
//call the DAL layer to create the cars linked to a user
carUser.CarUserMapping_Create(newUser.UserID, carIds);
so my first DAL method (which is a layer below the one this code is listed from) call creates the User - then once I have the user so I have the ID, etc I call another method at the DAL layer passing in the UserID of the newly created User and then the list of carIds associated with the User.
The code from the 2nd method is below:
public void CarUserMapping_Create(int userId, List<int> carIds)
{
using (CUEntities entities = new CUEntities())
{
User user = User_GetById(userId);
entities.Users.Attach(userId);
foreach (int carId in carIds)
{
Car car = Car_GetById(carId);
user.Cars.Add(car);
entities.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
entities.SaveChanges();
}
}
}
Can anyone see something I am doing wrong? My other option I am thinking is take this away from Entity Framework and just call a stored procedure in CarUserMapping_Create
Maybe you need to add do the following:
public void CarUserMapping_Create(int userId, List<int> carIds)
{
using (CUEntities entities = new CUEntities())
{
User user = User_GetById(userId);
entities.Users.Attach(userId);
foreach (int carId in carIds)
{
Car car = Car_GetById(carId);
car.UserID = user.UserID;
entities.Entry(car).State = EntityState.Modified;
entities.SaveChanges();
}
}
}
You shuold not set the state like that. thats like raping the framework.
Instedad send the model instance (entities) down as a paramater to the methods that reads the reads the car and user. (and ofc. use the entitiesas the ObjectContext in the methods)
like this:
using (CUEntities entities = new CUEntities())
{
User user = User_GetById(entities , userId);
foreach (int carId in carIds)
{
Car car = Car_GetById(entities, carId);
car.UserID = user.UserID;
entities.SaveChanges();
}
}
that way, the entity framework will keep tract of the entity state
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();