Though I am not trying to update primary key, I am getting this error message when I try to update Title field:
The property 'ProposalId' is part of the object's key information and cannot be modified.
ProposalId has been declared as Primary Key.
Here's the method : Context.Update<Proposal>(proposal, oldProposal);
proposal and oldProposal are instances of Proposal class and they both have different ProposalId.
proposal
ClientAddress: null
ClientCity: null
ClientCode: null
ClientCountry: null
ClientName: null
ClientState: null
CreatedBy: null
CreatedOn: {01/01/0001 00:00:00}
DateDead: {12/31/2016 00:00:00}
DateEngaged: {12/31/2016 00:00:00}
DatePlacedOnHold: {12/31/2016 00:00:00}
DateSubmitted: {12/31/2016 00:00:00}
EngProId: 3
EngRevenueRangeDesc: null
EngRevenueRangeId: 5
EngagementProbabilityDesc: null
ExpectedEngDate: {12/31/2016 00:00:00}
IsActive: true
IsIntegrated: null
Key: 512
LessonsLearned: "Lessons learned"
NonEngRevenueRangeDesc: null
NonEngRevenueRangeId: 1
Notes: Count = 0
OfficeCode: null
OfficeName: null
OriginalProposalNumber: "1700194P2"
PaymentTerms: "30"
Project: null
ProjectCode: "1700194"
ProjectSite: null
ProposalDueDate: {10/21/2016 00:00:00}
ProposalGeneralInfo: Count = 2
ProposalId: 512
ProposalMethodofExecutions: Count = 1
ProposalNumber: "1700194P2V1"
ProposalPractices: Count = 2
ProposalProjectPhases: Count = 1
Resources: Count = 2
RevisionNum: 1
Revisions: Count = 0
RiskLevel: null
RiskLevelAccessibility: Deny
SalesProjectCode: "STL-SALES-16"
ScopeDescription: "scope description"
StatusCode: "D"
StatusName: null
TaskUID: null
Title: "test proposal update-1 Jan 12 2017"
TotalCost: 0
TotalHours: null
TotalRevenue: null
UpdatedBy: "krajaraja"
UpdatedOn: {01/12/2017 04:16:24}
WorkAllocations: Count = 1
oldProposal
ClientAddress: "P.O. Box 465662"
ClientCity: "Cincinnati"
ClientCode: null
ClientCountry: "USA"
ClientName: null
ClientState: "OH"
CreatedBy: "krajaraja"
CreatedOn: {01/09/2017 04:11:20}
DateDead: {01/09/2017 05:13:38}
DateEngaged: {12/31/2016 00:00:00}
DatePlacedOnHold: {12/31/2016 00:00:00}
DateSubmitted: {12/31/2016 00:00:00}
EngProId: 3
EngRevenueRangeDesc: "Place Holder"
EngRevenueRangeId: 5
EngagementProbabilityDesc: "70% - Pipeline, Likely to Receive"
ExpectedEngDate: {12/31/2016 00:00:00}
IsActive: true
IsIntegrated: null
Key: 510
LessonsLearned: "Lessons learned"
NonEngRevenueRangeDesc: "Place Holder"
NonEngRevenueRangeId: 1
Notes: Count = 8
OfficeCode: null
OfficeName: null
OriginalProposalNumber: "1700194P2"
PaymentTerms: "30"
Project: {BWDG.Web.EMS.Proposals.Data.Models.Project}
ProjectCode: "1700194"
ProjectSite: "P.O. Box 465662, Cincinnati, OH, USA"
ProposalDueDate: {10/21/2016 00:00:00}
ProposalGeneralInfo: Count = 11
ProposalId: 510
ProposalMethodofExecutions: Count = 8
ProposalNumber: "1700194P2"
ProposalPractices: Count = 9
ProposalProjectPhases: Count = 6
Resources: Count = 2
RevisionNum: 0
Revisions: Count = 4
RiskLevel: 5
RiskLevelAccessibility: Deny
SalesProjectCode: "STL-SALES-16"
ScopeDescription: "scope description"
StatusCode: "X"
StatusName: "Dead"
TaskUID: null
Title: "test proposal update-1 Jan 09 2017"
TotalCost: 0.00
TotalHours: null
TotalRevenue: null
UpdatedBy: "krajaraja"
UpdatedOn: {01/09/2017 05:13:55}
WorkAllocations: Count = 1
This implementation throwing the exception:
public void Update<T>(T entity, T oldEntity = null) where T : class, IEntity
{
if (entity == null) throw new ArgumentException("entity");
var entry = this.Entry<T>(entity);
if (entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
{
if (oldEntity != null)
{
var attachedEntry = this.Entry(oldEntity);
attachedEntry.CurrentValues.SetValues(entity);
}
else
{
entry.State = EntityState.Modified; // This should attach entity
}
}
}