I have two table Subscription and PackageType. Subs has FK as PackageTypeId. Now when I am inserting a new record in Subscription table using EF 4.1 it is throwing an exception
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Subscription_PaymentType". The conflict occurred in database “MyDatabaseName”, table "dbo.PaymentType", column 'Id'.
The statement has been terminated.
Tables information are:
Subscription Table:
Id (PK)
PaymentTypeId (FK)
Period
date
PaymentType:
Id (PK)
Name
And the Code is as given below:
public void Proceed(SubscriptionSessionData data)
{
if (data != null)
{
PMSCatalogEntities entities = new PMSCatalogEntities();
Subscription subs = new Subscription();
subs.Id = Guid.NewGuid();
subs.ApplicableFrom = data.ApplicableFrom;
subs.TenantId = tenant.Id;
subs.PackageId = data.PaymentType;
subs.PaymentTypeId = data.PaymentType;
entities.AddToSubscriptions(subs);
entities.SaveChanges();
}
}
Any idea about this issue?
I have already tested this scenario and it works great:
And on the DB
The only thing I have to be sure is that a Payment with Id 1 for this example is persisted on the Payments table, and it works like a charm!!