Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A dependent property in a ReferentialConstraint is mapped to a store-generated column

I get this error when writing to the database:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'PaymentId'.

public bool PayForItem(int terminalId, double paymentAmount, 
      eNums.MasterCategoryEnum  mastercategoryEnum, int CategoryId, int CategoryItemId)
    {

        using (var dbEntities = new DatabaseAccess.Schema.EntityModel())
        {
            int pinnumber = 0;
            long pinid = 1; //getPinId(terminalId,ref pinnumber) ;
            var payment = new DatabaseAccess.Schema.Payment();
            payment.CategoryId = CategoryId;
            payment.ItemCategoryId = CategoryItemId;
            payment.PaymentAmount = (decimal)paymentAmount;
            payment.TerminalId = terminalId;
            payment.PinId = pinid;

            payment.HSBCResponseCode = "";
            payment.DateActivated = DateTime.Now;
            payment.PaymentString = "Payment";
            payment.PromotionalOfferId = 1;
            payment.PaymentStatusId = (int)eNums.PaymentStatus.Paid;

            //payment.PaymentId = 1;

            dbEntities.AddToPayments(payment);
            dbEntities.SaveChanges();
        }
        return true;
    }

The schema is:

enter image description here

like image 338
Welsh King Avatar asked Jun 17 '11 10:06

Welsh King


3 Answers

Is it possible that you defined a bad column relation between your tables?

In my case, I had different columns and one was set as autonumeric.

like image 115
ju4nj3 Avatar answered Oct 22 '22 22:10

ju4nj3


This error says that you are using unsupported relation or you have error in your mapping. Your code is most probably absolutely unrelated to the error.

The error means that you have some relation between entities where foreign key property in dependent entity is defined as store generated. Store generated properties are filled in the database. EF doesn't support store generated properties as foreign keys (as well as computed properties in primary keys).

like image 33
Ladislav Mrnka Avatar answered Oct 22 '22 22:10

Ladislav Mrnka


I had the same problem. Based on the answers provided here I was able to track it and solve it, but I had a strange issue described below - it might help somebody in the future.

On my dependent tables, the foreign Key columns have been set to StoreGeneratedPattern="Identity". I had to change it to "None". Unfortunately, doing so inside designer didn't work at all.

I looked in the designer-generated XML (SSDL) and these properties were still there so I removed them manually. I also had to fix the columns on the database (remove the Identity(1,1) from CREATE TABLE SQL)

After that, the problem went away.

like image 9
surfen Avatar answered Oct 22 '22 23:10

surfen