2014年7月13日 星期日

[RESOLVED] LINQ and Entity Framework in ASP.net 4.0 / C#


I have a user respoistory and a role respository.  In my user repo I update and insert records. In the update part, I deleted all the user roles and then re add them based on a role dto object.  Is there a way to pass my context of Project Entities class
into both repositories and have it update once compared to do .SaveChanges() in each function?


If I am not explaining this well enough let me know please.  Basically I want to add all the task that need to be done in proper order to my context, but in different class files.



Yes


Create the instance in one repository and pass it in the constructor when instantiating the other  repository.


Now do the changes and call db.SaveChanges() from the initial repo





syed.iddi



Yes


Create the instance in one repository and pass it in the constructor when instantiating the other  repository.


Now do the changes and call db.SaveChanges() from the initial repo





I didn't add this, but all of them inherit a base repository which is where the Entity class is declared. I believe in the contructor for that it makes a new one. I guess instead of making a new one for this contructor I just pass in what is already set?



Yes, have overloaded constructor





syed.iddi



Yes, have overloaded constructor





Okay let me know if this is close to what I need?  I added some comments in the files of the TestRepoOne and TestRepoTwo to show where I commented out .SaveChanges()


TestBaseRepository.cs


using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Repositories
{
public class TestBaseRepository
{
protected TestEntities TestContext;

public TestBaseRepository()
{
TestContext = new TestEntities();
}

public TestBaseRepository(TestEntities entityParam)
{
TestContext = entityParam;
}
}
}

TestRepoOne.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Models;

namespace DAL.Repositories
{
public class TestRepoOne : TestBaseRepository
{
public TestRepoOne()
{
// ....
}

public TestRepoOne(TestEntities entityParam) : base (entityParam)
{

}

public bool DeleteRoles()
{

TestContext.tbl_family_names.Where(x => x.P_Id == 2).ToList().ForEach(TestContext.tbl_family_names.DeleteObject);

// So now since I have an overloaded contructor I will not have to call
// The below code, just call it in the other Repository???

//TestContext.SaveChanges();

return true;
}


public bool AddRoles()
{
IList theList = new List();

theList.Add(999);
theList.Add(1014);
theList.Add(1024);

foreach (short item in theList)
{

tbl_family_names addRoles = new tbl_family_names()
{
P_Id = item
};


TestContext.AddTotbl_family_names(addRoles);

// So now since I have an overloaded contructor I will not have to call
// The below code, just call it in the other Repository???

//TestContext.SaveChanges();

}

return true;
}
}
}


TestRepoTwo.cs


using DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL.Repositories
{
public class TestRepoTwo : TestBaseRepository
{
public TestRepoTwo()
{
// ....
}

public TestRepoTwo(TestEntities entityParam)
: base(entityParam)
{

}


public bool UpdateRecord()
{

TestRepoOne testRepoOne = new TestRepoOne(TestContext);

bool resultDeleteRoles = testRepoOne.DeleteRoles();

bool resultAddRoles = testRepoOne.AddRoles();


// Now I can call save changes here, so if anything happen and fails before this code
// (even if I have already deleted the roles etc and the error was when they were being added)
//, then it will roll back everything before it declares that there is an error and re add those deleted roles back?
TestContext.SaveChanges();


return true;
}
}
}




Yes. This is what I mentioned.


The idea is to have the same datacontext in both the repositories. You can also use public properties to set the datacontext. Call the save changes from the calling repository.


did you try this out? any issues?





syed.iddi



Yes. This is what I mentioned.


The idea is to have the same datacontext in both the repositories. You can also use public properties to set the datacontext. Call the save changes from the calling repository.


did you try this out? any issues?






It compiled with no issues.  I will have to try it later when I can in the actual code environment.  WHen I do, I will post my results!  Thanks for the help. I will be sure to check your answer as good once I validate. 




syed.iddi - this seemed to work like a charm thanks.


沒有留言:

張貼留言