how to skip object when submitchanges in ado.net - c#

I'm using ado.net entity data model. I have 2 object User Organization
My problem is
User user=new User();
...
Organization org=new Organization()
db.Organizations.AddObject(org);
db.SubmitChanges(); // Problem is here. Because datacontext try to save user object. user object some field is empty. I don't need to save user here. How to skip user object from submitchanges
user.organization=org;
db.SubmitChanges(); // I need to save user object here

You need a using statement to scope out your data context.
Not exactly sure what you are trying to achieve, but something like this?
Organization org=new Organization();
User user = new User();
using (var ctx = new YourContext())
{
ctx.Organizations.InsertOnSubmit(org);
ctx.SubmitChanges();
user.Organization = org;
}
using (var newCtx = new YourContext())
{
// code to persist user
}

Related

Inserting a row into Relational database table

I am very new to c# and asp.net mvc. I'm building a HR portal for our company where a user can submit a leave form among other things... So I'm using mssql as the database server and using Entity Frame work to communicate with it. I have 3 entities, user (Containing user details), permissions (the user permissions for allowing actions in the app) and then the leave form table (where the leave form details are stored). There is a one to many relationship between user - permission and then a one to many relationship between user-leave. I am not fazed about the permissions as that gets created when the user account is being created.
The problem I am facing is, how do I add a leave form for a specific user? Below is my controller code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Leave(MasterViewModel model)
{
DocSubViewModel mv = model.DSModel;
int userId = Convert.ToInt32(Session["userID"]);
try
{
using (HrDcpDBContainer db = new HrDcpDBContainer())
{
var leave = db.leaves.Create();
leave.dateFrom = mv.DateFrom;
leave.dateSubmitted = DateTime.Now;
leave.dateTo = mv.DateTo;
leave.nrDays = mv.NrDays;
leave.reason = mv.SpecialLeave;
leave.TLApproval = null;
leave.TLApprovalDate = null;
leave.TLApprovalID = mv.TeamLeaderID;
leave.DMApprovalDate = null;
leave.DMApprovalID = mv.DepManagerID;
leave.DMApproval = null;
leave.type = mv.Type;
leave.user = userId;
db.leaves.Add(leave);
db.SaveChanges();
}
ViewBag.Message = "Leave Form submitted Successfully. You will be redirected shortly...";
return View("result");
}
catch (Exception ex)
{
ViewBag.Message = ex;
//ViewBag.Message = "Leave Form submitted Successfully. You will be redirected shortly...";
return View("result");
}
The problem comes in leave.user = userId;. It says:
Cannot implicitly convert int to Portal.Model.user
I can't seem to find out how to do this...
You're telling it to put the UserId where your leave model is asking for a User.
Your relationship requires a User to go in there, so you'll have to update your code a little bit:
using (HrDcpDBContainer db = new HrDcpDBContainer())
{
var leave = db.leaves.Create();
leave.user = db.users.First(x => x.Id == userId);
}
This will put reference to the actual user in the new leave record. If you go later and check it out you'll see a column in the leave table called user_Id that has an integer value in it and is set as a foreign key to the users table.
Note that this will error if no user exists having the specified Id value. If you anticipate this to be a problem, rather use .FirstOrDefault() instead of .First() and then account for the value being null before you add it to your new leave object.
That's expected since User is a object and not int. What you should be doing probably is leave.user.UserId = userId; instead [Assuming leave.user is of type User which has a UserId property]

Entity Framework: adding existing child POCO to new Parent POCO, creates new child in DB

Im wanting to use Entity Framework POCO in a disconnected (from context) mode. In my scenario I'm creating a new Parent object and want to attach an existing child object to it and then save it to the db.
The code below undesirably inserts a new Course record when saving a new Student record, when instead I want the existing Course record linked to the new Student record.
How can I do this in Entity Framework where...
the objects can be disconnected from the context. (i.e. Queried in one context and then saved in a different context)
I dont need to re-query the child record from the DB just so I can attach it to the parent when I'm saving to db. I really want to avoid doing extra trips to the db when I already have it as an object in memory.
This page shows a database diagram that the code below is based on http://entityframeworktutorial.net/EF4_EnvSetup.aspx#.UPMZ4m-UN9Y
class Program
{
static void Main(string[] args)
{
//get existing course from db as disconnected object
var course = Program.getCourse();
//create new student
var stud = new Student();
stud.StudentName = "bob";
//assign existing course to the student
stud.Courses.Add(course);
//save student to db
using (SchoolDBEntities ctx = new SchoolDBEntities())
{
ctx.Students.AddObject(stud);
ctx.SaveChanges();
}
}
static Course getCourse()
{
Course returnCourse = null;
using (var ctx = new SchoolDBEntities())
{
ctx.ContextOptions.LazyLoadingEnabled = false;
returnCourse = (from s in ctx.Courses
select s).SingleOrDefault();
}
return returnCourse;
}
}
I believe there are few ways of accomplishing this.
You can specify that course entity is unchanged rather than added, along these lines:
ctx.Entry(course).State = EntityState.Unchanged;
Or instruct your context, that you are working with existing entity:
ctx.Courses.Attach(course);
More info here:
http://msdn.microsoft.com/en-us/data/jj592676.aspx
EDIT
There are some running samples from my solution, I verified they work as expected.
In all cases we have Publisher record in database with ID = 2 and Name = "Addison Wesley" (irrelevant to the example, but just for good measure).
Approach 1 - Setting Entity State
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.Publisher = new Publisher() {Id = 2 }; // Only ID is required
context.Entry(book.Publisher).State = EntityState.Unchanged;
context.Books.Add(book);
context.SaveChanges();
}
Approach 2 - Using Attach method
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.Publisher = new Publisher() { Id = 2 }; // Only ID is required
context.Publishers.Attach(book.Publisher);
context.Books.Add(book);
context.SaveChanges();
}
Approach 3 - Setting Foreign Key value
using (var context = new Context())
{
var book = new Book();
book.Name = "Service Design Patterns";
book.PublisherId = 2;
context.Books.Add(book);
context.SaveChanges();
}
For this last approach to work I needed to add extra property PublisherId, it has to be named according to NavigationPropertyName + 'Id" convention to be picked up by EF auotmatically:
public int PublisherId { get; set; }
public Publisher Publisher { get; set; }
I am using here EF5 Code First, but it is very similar to POCO.
Entity Framework does not allow relationships that cross contexts.
If you place the reading of the course and connecting the course to the student within the same using statement, it would work.
I also tried the second option it worked for me. I did like the parent->child relationship happening at an object level first and save to db. Maybe I should just remove all the relationships between the entities that EF generates and manually control this myself.

Can't update an entity with Poco Code Generation Item

I have the following scenario:
the below 2 global variables in my page:
MainContext db = new MainContext();
User user = new User();
A method to fill the properties that is called on the button update:
private void FillProperties()
{
user.FirstName = txtFirstName.Text;
user.LastName = txtLastName.Text;
user.Email = txtEmail.Text;
}
on the button update i am doing the following:
FillProperties();
user.ID = Request.QueryString("userid");
db.SaveChanges();
but the record isn't been updated which i think is logical since the user variable isn't related to db object, but i a, not being able to do this:
db.Users.AddObject(user);
since AddObject isn't found in the db properties.
What can I do to update user?
Your answers are appreciated.
If you are using the DbContext API simply use
db.Entry(user).State = EntityState.Modified;
Where do you add your user to the DB?
If the user exists in the DB, in order to update it, you should do, for example:
user = db.Users.Where(x => x.ID == Request.QueryString("userid")).Single();
FillProperties();
db.SaveChanges();

EF4.1 Code First Add Entity with Related Entity

I'm using EF4.1 Code First. I can't get a very simple insert to save the related entity ID. The generated SQL always inserts NULL for any related entities. Example code is below. Can anyone see what I'm doing wrong here? It does properly insert non-entity properties, such as strings. Also I have similar code in a DB initializer class to seed test data and it seems to work fine.
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
ctx.MyItems.Add(newItem);
// set related entity
newItem.CreatedBy = existingUser;
ctx.SaveChanges();
}
Your code should work with default configuration of your DbContext. One possible explanation that it does not work is that you have automatic change detection disabled, for instance if you have in your context's constructor something like:
public DataContext()
{
this.Configuration.AutoDetectChangesEnabled = false;
}
In this case EF would not detect the change of the navigation property newItem.CreatedBy after you have added the new item to the context. (SaveChanges would detect this last change if change detection isn't disabled.)
You can change your code so that setting the navigation property happens before you add the new item to the context:
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
// set related entity
newItem.CreatedBy = existingUser;
ctx.MyItems.Add(newItem);
ctx.SaveChanges();
}
This should work with and without automatic change detection.
Try this:
using (var ctx = new DataContext())
{
ctx.Users.Attach(existingUser);
// create item and add to context
var newItem = new MyItem();
ctx.MyItems.Add(newItem);
// set related entity
newItem.CreatedBy = existingUser;
// Added
ctx.ObjectStateManager.ChangeObjectState(newItem.CreatedBy, EntityState.Added);
ctx.SaveChanges();
}
If that doesn't work, change the line with:
ctx.ObjectStateManager.ChangeObjectState(newItem.CreatedBy, EntityState.Modified);
Added 1 line... Hope it helps.

How to add/remove many-to-many relation with entity framework by entity key?

I tried:
using (Entities e = new Entities())
{
EntityKey key = new EntityKey("Entities.Users", "UserId", 20);
User user = new User { EntityKey = key};
Role role = e.Roles.FirstOrDefault();
//role.Users.Attach(user); //throws (when uncommented...) InvalidOperationException:
//The object being attached to the source object is not attached to the same ObjectContext as the source object.
role.Users.Add(user); //throws InvalidOperationException too:
//The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.
e.SaveChanges();
}
When trying to use Remove() without calling attach before no exception is thrown but relation not deleted.
Try something like this:
User user = new User {UserId = 20};
e.AttachTo("Users", user);
Role role = e.Roles.FirstOrDefault();
role.Users.Add(user);
e.SaveChanges();
I find it much easier to work with Stub Entities (like the above user) rather than EntityKeys.
See this blog post for more info on Stub Entity techniques.
Hope this helps
Alex

Categories