0

Every time I try to save something into the database I get the error:

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

Following is the code that does this, the line DB.SaveChanges();.

public ActionResult CreatePackage(Package package, string MaterialId, string SubCategoryId, string Companies)
        {
            ModelState.Clear();

            //add stuff to package
            package.CreatedDate = DateTime.Now;

            DB.Packages.Add(package);
            DB.SaveChanges();

            return Redirect(Url.Action("Index", "Material") + "#Packages");
        }

Below is the diagram of the table that throws the exception:

enter image description here

After doing some heavy reasearch I on stackoverflow I believe that there might be something wrong with my foreign keys:

However I can't see to find what can possibl be wrong with my code, nor my DB. I am trying to create a many-to-many relationship between the table Packages and COmpany. A Company can be in several packages, and a packes can be used by several companies.

What I find odd, is that this only happens when I try to create a new object. For example, If I have a Package with PackageId = 0 and a Company with CompanyId = 0 and if I run the SQL server statement:

INSERT INTO GATE_Package_Company
VALUES (0, 0);

Everything works fine because the previous object already exist. But if I try to create a new Pacakge object (one that does not exist yet) and I try to save it, I get the error. Right now it is simply not possible to add new packages :S

What am I missing?

Community
  • 1
  • 1
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266

1 Answers1

0

The problem was on Visual Studio. By deleting the table from the ´.edmx´ file and re-importing it from the database again, the problem was fixed.

I am using Visual Studio Ultimate 2013, and I have no idea if this problem is particular to this version or if it also affects other versions.

All I know is that I wasted 2 days with this. I honestly hope this answer can help someone else in the future.

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266