Hi!
First of all thanks for your quick feedback.
You were right about the missing save, but it was saving I just paste my code incorrectly to the message.
I tried your code, but I must be missing something since I got the following error:
Cannot insert the value NULL into column 'EmployeeId', table 'CompanyManager.dbo.EmployeeFeatures'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The table EmployeeFeatures relates Employees with Features by a double primary key composed by EmployeeId and FeatureId which are, respectively, foreign keys to the tables Employee and Feature.
When I do:
new EmployeeFeaturesEntity { FeatureId = featureId, IsNew = true }
I am not stating the EmployeedId, but then again I don't even know it, since this is a brand new Entity; but still I thought this would be filled internal and automatically.
What am I doing wrong?
Thanks in advance.
kind regards,
Vitor Varalonga
daelmo wrote:
What is ctx? I presume it's a DataAccessAdapter. You started a transaction but you are not saving anything. You need to save employee explicitly, in fact if you save it recursively (which will save all the related Employee features as well) you don't need to use a transaction, as a recursive save instantiate a transaction inside. Use a transaction if there is more than one .Save action you want to keep together.
So, do something like:
ctx.StartTransaction();
try
{
var employee = new EmployeeEntity { IsNew = true };
employeeFeatures.ForEach(featureId =>
employee.EmployeeFeatures.Add(
new EmployeeFeaturesEntity { FeatureId = featureId, IsNew = true }
)
);
ctx.SaveEntity(employee, true, true);
ctx.Commit();
}
catch(Exception ex)
{
ctx.Rollback();
throw;
}
or...
ctx = new DataAccessAdapter
try
{
var employee = new EmployeeEntity { IsNew = true };
employeeFeatures.ForEach(featureId =>
employee.EmployeeFeatures.Add(
new EmployeeFeaturesEntity { FeatureId = featureId, IsNew = true }
)
);
ctx.SaveEntity(employee, true, true);
}
catch(Exception ex)
{
throw;
}