Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Brief intro about the entire structure of what was done before the actual question.

It is required to have Custom Primary keys for all transactions. To create the key, store procedure with an input parameter named "SiteCode" is used. This store procedure creates sequence keys for every transaction made in the application. For every transaction insert there is a Stored procedure and this sp's calls key generating SP before any insert statements. The generated key is the primary key to the insert statement. Thus the actual "SiteCode" has been to every transaction SP's as an input parameter. And have created DB first entity Model and also all the entity class has been created

There is a screen in WPF which capture the data for the models Customer, Address, Contact information.
Instead of getting model one by one and saving . It would be better to map the stored procedure function (used to insert the values) with respective model.
(e.g) CutomerRef attribute to sp_CreateCustomer SP in entity framework

During this process, mapping was done on all fields except except the "SiteCode" field which does not have any reference in any entity model.

Already tried creating partial class to the cutomerRef model,adding this property into it and try to map with SP. But no success.
Another failed attempt was creating the field in the entity model and mapping with the sp..

Its a known fact that this can be done by calling sp from stored procedure but would prefer to do just by Context.Customer.Add(customerModel)

Any help would be appreciated
Posted

1 solution

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

C#
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 ProjectID
{
    get
    {
        return _ProjectID;
    }
    set
    {
        OnProjectIDChanging(value);
        ReportPropertyChanging("ProjectID");
        _ProjectID = StructuralObject.SetValidValue(value, "ProjectID"); // change this line.
        ReportPropertyChanged("ProjectID");
        OnProjectIDChanged();
    }
}


change it to

C#
[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"); // here just call the static method
        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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900