I would suggest you to try following. Not very clean approach but I am sure it will work.
It requires that you change your
YourDatabase.Designer.cs
file (the file you see when you expand your YourDatabase.edmx in Solution Explorer).
But before that create a static method in C# that calls you
sp_GeneratePrimaryKey
StoredProcedure with
SiteCode
parameter. let's assume that method body is
Utilities.NewPrimaryKey(int SiteCode)
Find the property representing your Primary Key in the file mentioned above. i.e.
ProjectID
it should look like this
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 ProjectID
{
get
{
return _ProjectID;
}
set
{
OnProjectIDChanging(value);
ReportPropertyChanging("ProjectID");
_ProjectID = StructuralObject.SetValidValue(value, "ProjectID");
ReportPropertyChanged("ProjectID");
OnProjectIDChanged();
}
}
change it to
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 ProjectID
{
get
{
return _ProjectID;
}
set
{
OnProjectIDChanging(value);
ReportPropertyChanging("ProjectID");
_ProjectID = StructuralObject.SetValidValue(Utilities.NewPrimaryKey(value), "ProjectID");
ReportPropertyChanged("ProjectID");
OnProjectIDChanged();
}
}
And once you do that just pass the
SiteCode
as a primary key value and it will call the Stored Procedure and set the correct value instead. This way you can call Context.Project.Add(projectModel)
I will post another answer if I think of a better way.